import pickle
import HTMLTagFilter
+import deterfile
+import getjpggeom
-sys.stdin = codecs.getreader('utf_8')(sys.stdin)
-sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
+#sys.stdin = codecs.getreader('utf_8')(sys.stdin)
+#sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
alist = ["a", "a:href", "a:name", "b", "br" ]
dlist = ["*"]
self._input_iter = None
self._index_past = {}
self._index = {}
+ self._image_dir = ""
def index_add(self, key, val):
"""
Add key and value to index.
@param key:
- @type key:
+ @type key:
@param val:
@key val:
def index_haskey(self, key):
return self._index.has_key(key)
- def markup(self, input_iter):
+ def markup(self, input_iter, release="0"):
"""
Do markup.
"""
self.input_iter = input_iter
self._page_counter = 1
+ self._image_border = 0
# alist = ["a", "a:href", "a:name", "b", "br" ]
# dlist = ["*"]
# tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
+ self._release = release
self.index_add("figs", [])
- anchor = ""
+ self._anchor = ""
for line in self.input_iter:
- line = self._default_markup_rule(line)
+ # line = self._default_markup_rule(line)
# head-of-line rules
if re.search(ur"^☆{{{$", line):
self._inline(line)
continue
+ elif re.search(ur"^☆image_dir:", line):
+ self._image_dir = re.search(ur"^☆image_dir:\s*(.*)$", line).group(1)
+ continue
+ elif re.search(ur"^☆image_border:\s(on|On|ON)", line):
+ self._image_border = 1
+ continue
elif re.search(ur"^☆comment\s{{{$", line):
self._comment(line)
continue
elif re.search(ur"^☆\*", line):
self._anchor = re.sub(ur"^☆\*", "", line).strip()
continue
+ elif re.search(ur"^☆clear\s+", line):
+ self._clear(line)
+ continue
elif re.search(ur"^・", line):
self._ulist(line)
continue
+ elif re.search(ur"^[0-9]\.", line):
+ self._olist(line)
+ continue
elif re.search(ur"^☆begin-column:", line):
self._begin_column(line)
continue
elif re.search(ur"^☆end-column", line):
self._end_column(line)
continue
+ elif re.search(ur"^☆begin-note:", line):
+ self._begin_note(line)
+ continue
+ elif re.search(ur"^☆end-note", line):
+ self._end_note(line)
+ continue
elif re.search(ur"^☆space", line):
self._space(line)
continue
self._call_tables(line)
continue
elif re.search(ur"^●", line):
- if anchor != "":
- line = re.sub(ur"^●(.*)$", ur'<h4 id="%s">\1</h4>' % anchor, line)
- anchor = ""
- else:
- line = re.sub(ur"^●(.*)$", ur"<h4>\1</h4>", line)
- print line
+ self._head_l(line)
continue
elif re.search(ur"^○", line):
- if anchor != "":
- line = re.sub(ur"^○(.*)$", ur'<b id="%s">\1</b>' % anchor, line)
- anchor = ""
- else:
- line = re.sub(ur"^○(.*)$", ur"<b>\1</b>", line)
- print line
+ self._head_m(line)
+ continue
+ elif re.search(ur"^◇", line):
+ self._head_s(line)
continue
elif re.search(ur"^☆----", line):
- line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
- print line
+ self._newpage(line)
continue
elif re.search(ur"^☆\+---", line):
self._code(line)
elif re.search(ur"^☆図", line):
self._fig(line)
continue
+ elif re.search(ur"^☆写真", line):
+ self._photo(line)
+ continue
elif re.search(ur"^☆リスト", line):
self._list(line)
continue
+ elif re.search(ur"^☆実行例", line):
+ self._list(line)
+ continue
+ elif re.search(ur"^☆flow", line):
+ self._flow(line)
+ continue
if re.search(ur"^ ", line):
- line = "<p>" + line + "</p>"
+ self._paragraph(line)
+ continue
+
if re.search(r"^\s*$", line):
line = ""
+ line = line.strip()
print line
# end-of-loop
+ def _clear(self, line):
+ print """<div style="clear:left;"> </div>
+"""
+
+ def _head_l(self, line):
+ line = line.rstrip()
+ if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
+ self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
+ line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
+
+ line = self._default_markup_rule(line)
+ if self._anchor != "":
+ line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
+ self._anchor = ""
+ else:
+ line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
+ print line
+
+ def _head_m(self, line):
+ line = line.rstrip()
+ if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
+ self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
+ line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
+
+ line = self._default_markup_rule(line)
+ if self._anchor != "":
+ line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
+ self._anchor = ""
+ else:
+ line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
+ print line
+
+ def _head_s(self, line):
+ line = line.rstrip()
+ if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
+ self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
+ line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
+
+ line = self._default_markup_rule(line)
+ if self._anchor != "":
+ line = re.sub(ur"^◇(.*)$", ur'<div id="%s"><h5>\1</h5></div>' % self._anchor, line)
+ self._anchor = ""
+ else:
+ line = re.sub(ur"^◇(.*)$", ur"<h5>\1</h5>", line)
+ print line
+
+ def _paragraph(self, line):
+ line = self._default_markup_rule(line)
+ line = "<p>" + line + "</p>"
+ print line
+
+ def _newpage(self, line):
+ line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
+ print line
def load_index(self, path_to_index):
"""
def _call_tagles(self):
pass
+ def _escape(self, line):
+ line = re.sub(ur"&", ur"&", line)
+ line = re.sub(ur"<", ur"<", line)
+ line = re.sub(ur">", ur">", line)
+ return line
+
def _default_markup_rule(self, line):
"""
apply default markup rules.
@param line: string to apply markup
@type line: string
"""
- # line = re.sub(ur"&", ur"&", line)
- # line = re.sub(ur"<", ur"<", line)
- # line = re.sub(ur">", ur">", line)
+ line = self._escape(line)
# apply filter
# line = tag_filter.apply(line)
- line = re.sub(ur"[★*](表[0-9~]+)", ur"<b>\1</b>", line)
- line = re.sub(ur"[★*](図[0-9~]+)", ur"<b>\1</b>", line)
- line = re.sub(ur"[★*](リスト[0-9~]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](動画[0-9〜~、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](表[0-9〜~、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](図[0-9〜~、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](写真[0-9〜~、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](実行例[0-9〜~、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](リスト[0-9~〜、]+)", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*](コラム[0-9〜~、]+)", ur"<b>\1</b>", line)
line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
+ line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
+ line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
# comment
if re.search(ur"^☆#", line):
"""Proccess ul"""
print "<ul>"
while re.search(ur"^・", line):
+ line = self._default_markup_rule(line)
print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
line = self.input_iter.next()
print "</ul>\n"
+ def _olist(self, line):
+ """Proccess ul"""
+ print "<ol>"
+ while re.search(ur"^[0-9]+\.", line):
+ line = self._default_markup_rule(line)
+ print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
+ line = self.input_iter.next()
+ print "</ol>\n"
+
+
def _begin_column(self, line):
"""Proccess column"""
try:
except AttributeError:
str_title = ""
- html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
- <tr><th>%s</th></tr>
- <tr><td><span style="font-size: 85%%;">
+ html = """
+<div class="column" style="background:#DDDDDD;font-size:85%%;padding:8px;">
+<h4>%s</h4>
""" % (str_title)
print html
def _end_column(self, line):
- print """</span></td></tr>
- </table>
+ print """
+</div>
+ """
+
+ def _begin_note(self, line):
+ """Proccess note"""
+ try:
+ str_title = re.search(ur"^☆begin-note:(.*)$", line).group(1)
+ except AttributeError:
+ str_title = ""
+
+ html = """
+<div class="column" style="background:#F0F8FF;border:1px solid gray; font-size:85%%;padding:8px 8px 4px;margin-bottom: 1em;">
+"""
+ if len(str_title.strip()) > 0:
+ html = html + "<strong>%s</strong>" % (str_title)
+ print html
+
+ def _end_note(self, line):
+ print """
+</div>
"""
def _list_start(self):
def _list(self, line):
try:
- str_title = re.search("^☆(リスト.*)$", line).group(1)
+ str_title = re.search(ur"^☆((リスト|実行例).*)$", line).group(1)
except AttributeError:
str_title = ""
- print "<p><b>%s</b></p>" % (str_title)
- print self._list_start(line)
+ print "<p class='caption'><b>%s</b></p>" % (str_title)
+ print self._list_start()
for line in self.input_iter:
- line = line.strip()
- line = line.replace("&", "&")
- line = line.replace("<", "<")
- line = line.replace(">", ">")
- if line == "----":
+ line = line.strip("\n\r")
+ line = self._escape(line)
+ line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
+ line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
+ if re.search(ur"""^☆\+---""", line):
break
print line
print self._list_end()
print self._list_start()
for line in self.input_iter:
- # line = line.strip()
- line = line.replace("&", "&")
- line = line.replace("<", "<")
- line = line.replace(">", ">")
- line = line = re.sub(ur"★b\[(.*?)]", ur"<b>\1</b>", line)
+ line = self._escape(line)
+ line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
+ line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
+ line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
if re.search(ur"^☆\+---$", line):
break
print line,
print self._list_end()
-
def _inline(self, line):
for line in self.input_iter:
- # line = line.strip()
+ line = line.strip()
if re.search(ur"^☆}}}", line):
break
print line
-
def _comment(self, line):
for line in self.input_iter:
line = line.strip()
def _space(self, line):
print "<br><br>"
+ def _flow(self, line):
+ down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
+ flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
+"""
+ flow_title = """<div style="text-align:left; padding:4px 4px 4px 1em; margin-bottom: 1em; border-bottom: 1px solid; font-weight: bold; background-color:#BCD;">
+%s
+</div>"""
+ flow_footer = """</div>
+"""
+ flow_item = """<div>
+%s
+<p>%s</p>
+</div>
+
+"""
+ arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
+
+ rex_title = re.compile(ur"^☆flow\s+(.*)$")
+ if rex_title.search(line):
+ title = rex_title.search(line).group(1)
+ else:
+ title = ""
- def _fig_start(self, cap=""):
- return """<table align="center" border="0" cellpadding="0" cellspacing="0">
+ rex_file = re.compile(ur"^([^:]*):(.*)$")
+ outputs = []
+ for line in self.input_iter:
+ if re.search(r"^\s*$", line):
+ break
+ match = rex_file.search(line)
+ if match:
+ file = os.path.join(self._image_dir, match.group(1))
+ cap = self._default_markup_rule(match.group(2))
+ else:
+ continue
+ fig = self._anchored_fig(file, cap)
+ outputs.append(flow_item % (fig, cap))
+
+ print flow_header
+ print flow_title % (title,)
+ print arrow.join(outputs)
+ print flow_footer
+
+
+ def _fig_start(self, cap="", styles=[], width=0, height=0):
+ params = dict(style="", tablewidth="")
+ if width != 0:
+ params["style"] = "width:%d;" % (width,)
+ params["tablewidth"] = 'width="%d"' % (width,)
+
+ if "lfloat" in styles:
+ return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
<tr> <td valign="top" align="center">
- """
+ """ % params
+ elif "left" in styles:
+ return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
+ <tr> <td valign="top" align="center">
+ """ %params
+ else:
+ return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
+ <tr> <td valign="top" align="center">
+ """ % params
- def _fig_end(self, cap=""):
+ def _fig_end(self, cap="", styles=[]):
return """</td> </tr>
<tr> <td><span style="font-size: 80%%; font-weight: bold;">
%s
</table>
""" % (cap)
+ def _get_png_geom(self, filepath):
+ s = filepath.split('.')
+ ext = s[-1]
+ if (ext == 'JPG') or (ext == 'jpg'):
+ (w, h) = getjpggeom.get_jpeg_geometory(filepath)
+ return (w, h)
+ else:
+ desc = deterfile.file(filepath)
+
+ try:
+ m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
+ except IndexError:
+ err = ", ".join(desc)
+ raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
+ if m:
+ w = m.group(1)
+ h = m.group(2)
+ return (int(w), int(h))
+ else:
+ return None
+
def _fig(self, line):
try:
str_title = re.search(ur"^☆(図.*)$", line).group(1)
except AttributeError:
str_title = ""
+ if str_title.find(u"図*") == 0:
+ str_title = str_title.replace(u"図*", "")
+ line = self.input_iter.next()
+ styles = []
+ if line.find("@") == 0:
+ styles = line.strip().replace("@", "").split(",")
+ line = self.input_iter.next()
+
+ imgname = ""
+ imgname_s = ""
+ hash = ""
+ hash_s = ""
+ match_o1 = re.search(ur"<([^,]*?)>", line)
+ match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
+ if not match_o1 == None:
+ imgname = match_o1.group(1)
+ imgname = os.path.join(self._image_dir, imgname)
+ imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
+ elif not match_o2 == None:
+ imgname = match_o2.group(1)
+ imgname = os.path.join(self._image_dir, imgname)
+ imgname_s = match_o2.group(2)
+ imgname_s = os.path.join(self._image_dir, imgname_s)
+
+ try:
+ geom = self._get_png_geom(imgname_s)
+ except Exception, e:
+ sys.stderr.write(str(e) + "\nline: " + line.encode("utf-8"))
+ sys.exit(-1)
+
+ if geom:
+ w = geom[0]
+ h = geom[1]
+ print self._fig_start("", styles, width=w, height=h)
+ else:
+ print self._fig_start("", styles)
+ print self._anchored_fig(imgname, str_title, imgname_s)
+ print self._fig_end(str_title, styles);
+
+ dic = self.index("figs")
+ dic.append(imgname)
+ if imgname_s != "":
+ dic.append(imgname_s)
+
+ def _photo(self, line):
+ try:
+ str_title = re.search(ur"^☆(写真.*)$", line).group(1)
+ except AttributeError:
+ str_title = ""
+ if str_title.find(u"写真*") == 0:
+ str_title = str_title.replace(u"写真*", "")
+ line = self.input_iter.next()
+ styles = []
+ if line.find("@") == 0:
+ styles = line.strip().replace("@", "").split(",")
+ line = self.input_iter.next()
+
+ imgname = ""
+ imgname_s = ""
+ hash = ""
+ hash_s = ""
+ match_o1 = re.search(ur"<([^,]*?)>", line)
+ match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
+ if not match_o1 == None:
+ imgname = match_o1.group(1)
+ imgname = os.path.join(self._image_dir, imgname)
+ imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
+ elif not match_o2 == None:
+ imgname = match_o2.group(1)
+ imgname = os.path.join(self._image_dir, imgname)
+ imgname_s = match_o2.group(2)
+ imgname_s = os.path.join(self._image_dir, imgname_s)
+
+ geom = self._get_png_geom(imgname_s)
+ if geom:
+ w = geom[0]
+ h = geom[1]
+ print self._fig_start("", styles, width=w, height=h)
+ else:
+ print self._fig_start("", styles)
+ print self._anchored_fig(imgname, str_title, imgname_s)
+ print self._fig_end(str_title, styles);
+
+ dic = self.index("figs")
+ dic.append(imgname)
+ if imgname_s != "":
+ dic.append(imgname_s)
+
+
+ def _anchored_fig(self, file, alt, file_s=""):
+
+ if file_s == "":
+ file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
+
+ if not os.path.isfile(file_s):
+ file_s = file
+
+ alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
+
+ ret = """<a href="%s">
+ <img src="%s" alt="%s">
+</a>
+""" % (file, file_s, alt)
+
+
+ return ret
+
+
+ def _fig_release(self, line):
+ try:
+ str_title = re.search(ur"^☆(図.*)$", line).group(1)
+ except AttributeError:
+ str_title = ""
print self._fig_start()
line = self.input_iter.next()
hash_s = hash
print """<a href="/blob.pl?id=%s">
- <slash type="image" id="%s" title="%s">
- </a>
- """ % (hash, hash_s, str_title)
-
+ <slash type="image" id="%s" title="%s">
+ </a>
+ """ % (hash, hash_s, str_title)
+
dic = self.index("figs")
dic.append(imgname)
def _table_start(self, cap):
- return """<table align="center" border="1" width="90%%">
+ return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
<caption><b>%s</b></caption>
""" % cap
- def _table_end(self):
- return "</table>\n"
+ def _table_end(self, footnote=""):
+ return "</table>\n%s</div>\n" % (footnote,)
def _table(self, line):
str_title = ""
try:
str_title = re.search(ur"^☆(表.*)$", line).group(1)
- fig_name = re.search(ur"^☆(表[0-9A-Z]+)", line).group(1)
+ fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
except AttributeError:
str_title = ""
fig_name = ""
+ if str_title.find(u"表*") == 0:
+ str_title = str_title.replace(u"表*", "")
+
print self._table_start(str_title)
self._table_buf1 = self._table_start(str_title)
num_row = 0
table_contents = []
+ footnote = ""
for line in self.input_iter:
line = line.strip(" \n")
+ line = self._default_markup_rule(line)
if re.search(ur"^\s*$", line):
break
- line = self._default_markup_rule(line)
+ if re.search(ur"^※", line):
+ footnote = re.search(ur"^(※.*)$", line).group(1)
+ break
if re.search(ur"^〓", line):
line = re.sub(ur"^〓", "", line)
tag_mode = "th"
table_contents[num_row][num_col-n]["col"] += 1
except IndexError:
pass
-
- table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
+ if item == u"↓":
+ n = 1
+ try:
+ while table_contents[num_row-n][num_col]["item"] == "":
+ n += 1
+ table_contents[num_row-n][num_col]["row"] += 1
+ item = ""
+ except IndexError:
+ pass
+
+ if re.search(r'^".*"$', item):
+ item = re.search(r'^"(.*)"$', item).group(1)
+ table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
+ else:
+ table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
num_col = num_col + 1
num_row = num_row + 1
# line = line.replace("\t", "</td><td>")
# print line
- print self._table_end()
+ print self._table_end(footnote)
self._table_buf1 = self._table_buf1 + self._table_end()
if self.index_haskey("tables"):
self.index("tables")[fig_name] = self._table_buf1