Skip to content

Commit

Permalink
Added ability to insert image size and width. Updated unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
fyngyrz committed Aug 6, 2015
1 parent 887bcb3 commit 39fb1f8
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ the readability of basic markup:

Desired Result | Markdown | macro\(\)
-------------- | -------- | -------
Italics | <tt>\*verbiage\*</tt> | <tt>\[i verbiage\]</tt>
Bold | <tt>\*\*verbiage\*\*</tt> | <tt>\[b verbiage\]</tt>
Paragraph | <tt>verbiage</tt> | <tt>\[p verbiage\]</tt>
List | <tt>\* item1</tt><br><tt>\* item2</tt> | <tt>\[ul item1,item2\]</tt><br>or, if you prefer,<br><tt>\[ul</tt><br><tt>item1,</tt><br><tt>item2</tt><br><tt>\]</tt><br>
Italics | `*verbiage\*` | `[i verbiage\]`
Bold | `**verbiage**` | `[b verbiage]`
Paragraph | `verbiage` | `[p verbiage]`
List | `* item1`<br>`* item2` | `[ul item1,item2]`<br><br>*or, if you prefer,*<br><br>`[ul`<br>`item1,`<br>`item2`<br>`]`<br>

Use is trivial:

Expand Down
140 changes: 129 additions & 11 deletions aa_macro.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/python

import re
import imghdr
import struct

class macro(object):
"""Class to provide an HTML macro language
Expand All @@ -17,8 +19,8 @@ class macro(object):
like, because our intellectual property system is pathological. The risks and
responsibilities and any subsequent consequences are entirely yours.
Incep Date: June 17th, 2015 (for Project)
LastRev: August 2nd, 2015 (for Class)
LastDocRev: August 2nd, 2015 (for Class)
LastRev: August 6th, 2015 (for Class)
LastDocRev: August 6th, 2015 (for Class)
Tab spacing: 4 (set your editor to this for sane formatting while reading)
Policies: 1) I will make every effort to never remove functionality or
alter existing functionality. Anything new will be implemented
Expand All @@ -44,8 +46,11 @@ class macro() is for authoring by you, the person who ISN'T trying to
someone who wants to do you wrong. Having said that, see the sanitize()
utility function within this class.
1st-Rel: 1.0.0
Version: 1.0.11
Version: 1.0.12
History: (for Class)
1.0.12
* added [locimg] to generate images with width and height set (jpg,png,gif)
* added [lipath] to tell [locimg] where image file is in local filesystem
1.0.11
* added [urlencode]
1.0.10
Expand Down Expand Up @@ -121,7 +126,23 @@ class macro() is for authoring by you, the person who ISN'T trying to
Images
------
[img (title,)URL( linkTarget)] # makes a link if linktarget present
[img (title,)URL( linkTarget)] # makes a link if linktarget present
[lipath localImagePath] # sets path to local images. This is used by [locimg] to
find the image and read it to obtain x,y dimensions
[lipath] first, then [locimg]
[locimg (title,)URL( linkTarget)] # makes a link if linktarget present, also inserts img size
# [locimg] can read the size of jpg, png, and gif
# Examples:
[lipath]
[locimg mypic.png] - image in same dir as python cmd on host
- and in / of webserver
[lipth /usr/www/mysite.com/htdocs/]
[locimg mypic.png] - image in /usr/www/mysite.com/htdocs/ on host
- and in / of webserver
[lipth /usr/www/mysite.com/htdocs/pics/]
[locimg pics/mypic.png] - image in /usr/www/mysite.com/htdocs/pics/ on host
- and in /pics/ of webserver
Lists
-----
Expand Down Expand Up @@ -308,6 +329,7 @@ def __init__(self,mode='3.2',back="ffffff",dothis=None,nodinner=False):
self.romans = ['m','cm','d','cd','c','xc','l','xl','x','ix','v','iv','i']
self.integers = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
self.result = ''
self.lipath = ''
if dothis != None:
self.do(dothis)

Expand All @@ -319,6 +341,59 @@ def setNoDinner(self,nd=False):
nd = True
self.noDinner = nd

def gis(self,fn):
def rhead(fn, length):
try:
fh = open(fn)
content = fh.read(length)
fh.close()
except:
return ''
if len(content) != length:
return ''
return(content)

x = 0
y = 0
insmell = '<'
moto = '>'
int4 = 'i'
uint16 = 'H'
try:
it = imghdr.what(fn)
except:
return 'unable to handle "%s"' % (fn,)
if it == 'jpeg':
try:
fh = open(fn)
sz = 2; ft = 0
while not 0xC0 <= ft <= 0xCF:
fh.seek(sz,1)
b = fh.read(1)
while ord(b) == 0xFF: b = fh.read(1)
sz = struct.unpack(moto + uint16,fh.read(2))[0]-2
ft = ord(b)
fh.seek(1,1)
y,x = struct.unpack(moto + uint16 + uint16, fh.read(4))
except:
try:
fh.close()
except:
pass
return 'unable to parse "%s"' % (fn,)
fh.close()
elif it == 'gif':
header = rhead(fn, 10)
if header == '': return '"%s" is pathological' % (fn,)
x,y = struct.unpack(insmell + uint16 + uint16, header[6:10])
elif it == 'png':
header = rhead(fn, 24)
if header == '': return '"%s" is pathological' % (fn,)
x,y = struct.unpack(moto + int4 + int4, header[16:24])
else:
return 'unhandled image type: "%s"' % (it,)
return x,y

def sanitize(self,
s, # input string
uwl='', # user whitelist
Expand Down Expand Up @@ -845,8 +920,33 @@ def color_fn(self,tag,data):
o += '<span style="background-color: #%s; color: #%s;">%s</span>' % (self.back,col,d2)
return o

def img_fn(self,tag,data):
def xyhelper(self,ifn):
xy = ''
tifn = ''
rfn = ifn[::-1]
run = True
for c in rfn:
if (run == True and
((c >= 'a' and c <= 'z') or
(c >= 'A' and c <= 'Z') or
(c >= '0' and c <= '9') or
(c == '-' or c == '_' or c == '.'))):
tifn = c + tifn
else:
run = False
plist = self.gis(self.lipath + tifn)
if len(plist) == 2:
xy = " width=%d height=%d" % (int(plist[0]),int(plist[1]))
return xy

def lipath_fn(self,tag,data):
self.lipath = data
return ''

def low_img_fn(self,tag,data,getxy=False):
tit = ''
txy = ''
rv = ''
try:
tit,d2 = data.split(',',1)
except:
Expand All @@ -857,11 +957,24 @@ def img_fn(self,tag,data):
d1,d2 = data.split(' ',1)
except:
if tit == '':
return '<img src="%s">' % (data)
return '<img alt="%s" title="%s" src="%s">' % (tit,tit,data)
if tit == '':
return '<a href="%s" target="_blank"><img src="%s"></a>' % (d2,d1)
return '<a href="%s" target="_blank"><img alt="%s" title="%s" src="%s"></a>' % (d2,tit,tit,d1)
if getxy == True: txy = self.xyhelper(data)
rv = '<img%s src="%s">' % (txy,data)
if rv == '':
if getxy == True: txy = self.xyhelper(data)
rv = '<img%s alt="%s" title="%s" src="%s">' % (txy,tit,tit,data)
if rv == '' and tit == '':
if getxy == True: txy = self.xyhelper(d1)
rv = '<a href="%s" target="_blank"><img%s src="%s"></a>' % (d2,txy,d1)
if rv == '':
if getxy == True: txy = self.xyhelper(d1)
rv ='<a href="%s" target="_blank"><img%s alt="%s" title="%s" src="%s"></a>' % (d2,txy,tit,tit,d1)
return rv

def img_fn(self,tag,data):
return self.low_img_fn(tag,data)

def locimg_fn(self,tag,data):
return self.low_img_fn(tag,data,True)

def web_fn(self,tag,data):
try:
Expand Down Expand Up @@ -1159,8 +1272,13 @@ def setFuncs(self): # '':self._fn,
'a' : self.a_fn, # [a (tab,)URL(,LinkedText)]
'link' : self.link_fn, # link URL (text) if no text, then linked URL
'web' : self.web_fn, # link URL (text) if no text, the "On the Web"
'img' : self.img_fn, # img emplacement from URL
'urlencode': self.urle_fn, # encodes space, ampersand, double quotes

# Image handling
# --------------
'img' : self.img_fn, # img emplacement from URL
'locimg': self.locimg_fn, # local image (tries for x y sizes)
'lipath': self.lipath_fn, # set local image path

# math
# ----
Expand Down
53 changes: 51 additions & 2 deletions expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,59 @@ <h2>linking</h2>
<p><a href="http://fyngyrz.com" target="_blank">Another Link</a></p>

<h2>Images</h2>
<h3>No size, no local lookup so no relative path</h3>
<p><a href="http://fyngyrz.com" target="_blank"><img src="http://fyngyrz.com/images/beachflag.png"></a></p>
<p><img src="http://fyngyrz.com/images/beachflag.png"></p>
<p><img title="Beach Flag" src="http://fyngyrz.com/images/beachflag.png"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img title="Beach Flag" src="http://fyngyrz.com/images/beachflag.png"></a></p>
<p><img alt="Beach Flag" title="Beach Flag" src="http://fyngyrz.com/images/beachflag.png"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img alt="Beach Flag" title="Beach Flag" src="http://fyngyrz.com/images/beachflag.png"></a></p>

<h3>Size, identical (empty) relative path</h3>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 src="test.png"></a></p>
<p><img width=291 height=407 src="test.png"></p>
<p><img width=291 height=407 alt=".png Test" title=".png Test" src="test.png"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 alt=".png Test" title=".png Test" src="test.png"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 src="test.jpg"></a></p>
<p><img width=207 height=346 src="test.jpg"></p>
<p><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="test.jpg"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="test.jpg"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 src="test.gif"></a></p>
<p><img width=40 height=40 src="test.gif"></p>
<p><img width=40 height=40 alt=".gif Test" title=".gif Test" src="test.gif"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 alt=".gif Test" title=".gif Test" src="test.gif"></a></p>

<h3>Size, relative path only</h3>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 src="test.png"></a></p>
<p><img width=291 height=407 src="test.png"></p>
<p><img width=291 height=407 alt=".png Test" title=".png Test" src="test.png"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 alt=".png Test" title=".png Test" src="test.png"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 src="test.jpg"></a></p>
<p><img width=207 height=346 src="test.jpg"></p>
<p><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="test.jpg"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="test.jpg"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 src="test.gif"></a></p>
<p><img width=40 height=40 src="test.gif"></p>
<p><img width=40 height=40 alt=".gif Test" title=".gif Test" src="test.gif"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 alt=".gif Test" title=".gif Test" src="test.gif"></a></p>

<h3>Size, relative path and http path</h3>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 src="randomdir/test.png"></a></p>
<p><img width=291 height=407 src="randomdir/test.png"></p>
<p><img width=291 height=407 alt=".png Test" title=".png Test" src="randomdir/test.png"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=291 height=407 alt=".png Test" title=".png Test" src="randomdir/test.png"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 src="randomdir/test.jpg"></a></p>
<p><img width=207 height=346 src="randomdir/test.jpg"></p>
<p><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="randomdir/test.jpg"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=207 height=346 alt=".jpg Test" title=".jpg Test" src="randomdir/test.jpg"></a></p>

<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 src="randomdir/test.gif"></a></p>
<p><img width=40 height=40 src="randomdir/test.gif"></p>
<p><img width=40 height=40 alt=".gif Test" title=".gif Test" src="randomdir/test.gif"></p>
<p><a href="http://fyngyrz.com" target="_blank"><img width=40 height=40 alt=".gif Test" title=".gif Test" src="randomdir/test.gif"></a></p>

<h2>Lists</h2>
<p><ul>
Expand Down
51 changes: 51 additions & 0 deletions mactest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,62 @@
[p [link http://fyngyrz.com Another Link]]

{h2 Images}
{h3 No size, no local lookup so no relative path}
[p [img http://fyngyrz.com/images/beachflag.png http://fyngyrz.com]]
[p [img http://fyngyrz.com/images/beachflag.png]]
[p [img Beach Flag,http://fyngyrz.com/images/beachflag.png]]
[p [img Beach Flag,http://fyngyrz.com/images/beachflag.png http://fyngyrz.com]]

{h3 Size, identical (empty) relative path}
[p [locimg test.png http://fyngyrz.com]]
[p [locimg test.png]]
[p [locimg .png Test,test.png]]
[p [locimg .png Test,test.png http://fyngyrz.com]]

[p [locimg test.jpg http://fyngyrz.com]]
[p [locimg test.jpg]]
[p [locimg .jpg Test,test.jpg]]
[p [locimg .jpg Test,test.jpg http://fyngyrz.com]]

[p [locimg test.gif http://fyngyrz.com]]
[p [locimg test.gif]]
[p [locimg .gif Test,test.gif]]
[p [locimg .gif Test,test.gif http://fyngyrz.com]]

{h3 Size, relative path only}
[lipath ./]
[p [locimg test.png http://fyngyrz.com]]
[p [locimg test.png]]
[p [locimg .png Test,test.png]]
[p [locimg .png Test,test.png http://fyngyrz.com]]

[p [locimg test.jpg http://fyngyrz.com]]
[p [locimg test.jpg]]
[p [locimg .jpg Test,test.jpg]]
[p [locimg .jpg Test,test.jpg http://fyngyrz.com]]

[p [locimg test.gif http://fyngyrz.com]]
[p [locimg test.gif]]
[p [locimg .gif Test,test.gif]]
[p [locimg .gif Test,test.gif http://fyngyrz.com]]

{h3 Size, relative path and http path}
[lipath ./]
[p [locimg randomdir/test.png http://fyngyrz.com]]
[p [locimg randomdir/test.png]]
[p [locimg .png Test,randomdir/test.png]]
[p [locimg .png Test,randomdir/test.png http://fyngyrz.com]]

[p [locimg randomdir/test.jpg http://fyngyrz.com]]
[p [locimg randomdir/test.jpg]]
[p [locimg .jpg Test,randomdir/test.jpg]]
[p [locimg .jpg Test,randomdir/test.jpg http://fyngyrz.com]]

[p [locimg randomdir/test.gif http://fyngyrz.com]]
[p [locimg randomdir/test.gif]]
[p [locimg .gif Test,randomdir/test.gif]]
[p [locimg .gif Test,randomdir/test.gif http://fyngyrz.com]]

{h2 Lists}
[p [ul item1,item2]]
[p [ol item1,item2]]
Expand Down
10 changes: 0 additions & 10 deletions martomac.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ def help():
lookformf = False
mfilename = el

#print str(argv)

usemfile = False
if mfilename != '':
try:
Expand Down Expand Up @@ -539,7 +537,6 @@ def smartUnEscape(s):
droptag = ''
floptag = ''
if github == True and line.find(' | ') > 0:
# print line
tline = ''
line = line.replace(r'\|',pipekey) # bury any escaped |'s
line = line.replace(',','{comma}') # intern commas -- we use 'em
Expand All @@ -564,19 +561,12 @@ def smartUnEscape(s):
line = tline
else:
if tmode != -1: # were we in a table?
print 'CLOSING: "%s"' % (line,)
tmode = -1 # then bail
floptag += '}\n' # close the table

# line = line.replace(r'\[','{lb}')
# line = line.replace(r'\]','{rb}')
# line = line.replace(r'\{','{ls}')
# line = line.replace(r'\}','{rs}')
# line = line.replace(',','{comma}')
if floptag != '':
source[-1] = source[-1] + floptag
source += ['\n']
# print source[-1]
source += [line]

OUTSTATE = 0
Expand Down
Binary file added test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 39fb1f8

Please sign in to comment.