OSDN Git Service

add command: function, add id
authorhylom <hylom@users.sourceforge.jp>
Mon, 16 Mar 2009 10:35:35 +0000 (19:35 +0900)
committerhylom <hylom@users.sourceforge.jp>
Mon, 16 Mar 2009 10:35:35 +0000 (19:35 +0900)
markup.py

index 2b97e92..751d560 100755 (executable)
--- a/markup.py
+++ b/markup.py
@@ -5,16 +5,22 @@ import sys
 import os
 import re
 import codecs
+import pickle
 
 import HTMLTagFilter
 
 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 = ["*"]
 
 tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
+path_to_index = "./_markup_index"
+
+index = {}
+index_past = {}
+
+page_counter = 1
 
 def make_hashlist(path_img_hash):
     """
@@ -65,22 +71,30 @@ def ulist(line):
         line = stream_in.readline()
     print "</ul>\n"
 
-def begin_column(line):
+def begin_column(line, anchor=""):
     try:
         str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
     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%%;">
-""" % (str_title)
+#    html = """<table id="%s" bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
+#<tr><th>%s</th></tr>
+#<tr><td><span style="font-size: 85%%;">
+#""" % (anchor, str_title)
+
+    add_anchor(anchor, str_title)
+
+    html = """<div id="%s" style="background: #DDDDDD; width=95%%; padding: 0.5em; margin-bottom: 1.5em;">
+<div style="text-align:center;"><b>%s</b></div>
+<div style="font-size:85%%">
+""" % (anchor, str_title)
     print html
 
 def end_column(line):
-    print """</span></td></tr>
-</table>
-"""
+    print """</div></div>"""
+#   print """</span></td></tr>
+#</table>
+#"""
 
 def list_start():
   return "<pre>"
@@ -189,7 +203,7 @@ def fig(line, filehash):
 
 
 def table_start(cap):
-    return """<table align="center" border="1" width="90%%">
+    return """<table align="center" border="1" width="90%%" class="table">
 <caption><b>%s</b></caption>
 """ % cap
 
@@ -268,13 +282,104 @@ def table(line):
 
     print table_end()
 
+def do_function_index(base_url):
+    if not index_past.has_key("anchors"):
+        return
+
+
+    indenting = 0
+    print '<table><tbody>'
+
+    for anchor, text, pagenum in index_past["anchors"]:
+        if pagenum == 1:
+            href = "#" + anchor
+        else:
+            href = base_url + "&pagenum=" + str(pagenum) + "#" + anchor
+
+
+        if re.search( ur"^●", text ):
+            print u'<tr><td colspan="2" style="padding-top:1em;padding-bottom:0.5em">%s:</td></tr>' % (text.replace(u"●", "", 1))
+
+        elif re.search( ur"^○", text):
+            str_item = re.sub(ur"^○コマンド[0-9]+:", "", text)
+            m = re.search(ur"「(.*)」.(.*)$", str_item)
+            str_command = m.group(1)
+            str_desc = m.group(2)
+            print '<tr><td style="padding-right:1em;"><strong><a href="%s">%s</a></strong></td><td><a href="%s">%s</a></td></tr>' % (href, str_command, href, str_desc)
+
+    print "</tbody></table>"
+
+
+
+def _do_function_index(base_url):
+    if not index_past.has_key("anchors"):
+        return
+
+
+    indenting = 0
+    for anchor, text, pagenum in index_past["anchors"]:
+        if pagenum == 1:
+            href = "#" + anchor
+        else:
+            href = base_url + "&pagenum=" + str(pagenum)
+
+        if re.search( ur"^●", text ):
+            if indenting > 1:
+                print "</li></ul></li>"
+            elif indenting < 1:
+                print '<ul type="none">'
+            else:
+                print "</li>\n"
+            indenting = 1
+            print ' '*indenting + '<li><a href="%s">%s</a>' % (href, text.replace(u"●", "", 1)),
+        elif re.search( ur"^○", text):
+            if indenting > 5:
+                print "</li></ul></li>"
+            elif indenting < 5:
+                print '<ul type="none">'
+            else:
+                print "</li>"
+            indenting = 5
+            print ' '*indenting + '<li><a href="%s">%s</a>' % (href, text.replace(u"○", "", 1)),
+        else:
+            if indenting > 10:
+                print "</li></ul></li>"
+            elif indenting < 10:
+                print '<ul type="none">'
+            else:
+                print "</li>"
+            indenting = 10
+            print ' '*indenting + '<li><a href="%s">%s</a>' % (href, text),
+    if indenting > 5:
+        print " </li></ul></li></ul></li></ul>"
+    elif indenting > 1:
+        print "</li></ul></li></ul>"
+    else :
+        print "</li></ul>"
+
+
+def do_function(line):
+
+    match_obj = re.search(ur"^☆function\((.*)\)", line)
+    if not match_obj:
+        return
+
+    func = match_obj.group(1)
+    args = func.split(",")
+    if args[0] == "index":
+        do_function_index(args[1])
+
+def add_anchor(anchor, text):
+    index["anchors"].append((anchor, text, page_counter))
 
 ####### main routine ##########
 
-str_usage = "markup.pl hashfile\n"
+# chekck argument and load filelist
+str_usage = "markup.pl hashfile targetfile\n"
 
 try:
     path_img_hash = sys.argv[1]
+    path_target = sys.argv[2]
 except IndexError:
     sys.stderr.write(str_usage)
     sys.exit(-1)
@@ -285,6 +390,23 @@ if hashlist == None:
     sys.stderr.write(str_usage)
     sys.exit(-1)
 
+# load index
+try:
+    index_file = open(path_to_index, "r")
+    index_past = pickle.load(index_file)
+    index_file.close()
+except IOError:
+    sys.stderr.write("warn: cannot read index file,\n")
+    index_past = {}
+
+file_target = codecs.open(path_target, "r", "utf_8" )
+
+anchor = ""
+index = {"file":path_target}
+index["anchors"] = []
+# TODO: don't use sys.stdin!
+sys.stdin = file_target
+
 for line in sys.stdin:
 
     line = default_markup_rule(line)
@@ -293,14 +415,20 @@ for line in sys.stdin:
     if re.search(ur"^☆{{{$", line):
         inline(line)
         continue
+    if re.search(ur"^☆function(.*)$", line):
+        do_function(line)
+        continue
     elif re.search(ur"^☆comment\s{{{$", line):
         comment(line)
         continue
+    elif re.search(ur"^☆\*", line):
+        anchor = re.sub(ur"^☆\*", "", line).strip()
+        continue
     elif re.search(ur"^・", line):
         ulist(line)
         continue
     elif re.search(ur"^☆begin-column:", line):
-        begin_column(line)
+        begin_column(line, anchor)
         continue
     elif re.search(ur"^☆end-column", line):
         end_column(line)
@@ -309,15 +437,26 @@ for line in sys.stdin:
         space(line)
         continue
     elif re.search(ur"^●", line):
-        line = re.sub(ur"^●(.*)$", ur"<h4>\1</h4>", line)
+        if anchor != "":
+            add_anchor(anchor, line.strip())
+            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
         continue
     elif re.search(ur"^○", line):
-        line = re.sub(ur"^○(.*)$", ur"<b>\1</b>", line)
+        if anchor != "":
+            add_anchor(anchor, line.strip())
+            line = re.sub(ur"^(.*)$", ur'<b id="%s">\1</b><br>' % anchor, line)
+            anchor = ""
+        else:
+            line = re.sub(ur"^(.*)$", ur"<b>\1</b><br>", line)
         print line
         continue
     elif re.search(ur"^☆----", line):
         line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
+        page_counter += 1
         print line
         continue
     elif re.search(ur"^☆\+---", line):
@@ -343,4 +482,10 @@ for line in sys.stdin:
 
 #end-of-loop
 
+# save index
+try:
+    index_file = open(path_to_index, "w")
+    pickle.dump(index, index_file)
+except IOError:
+    sys.stderr.write("warn: cannot write index file,\n")