From 417216c786bbc9c3a005849e77763e5640340256 Mon Sep 17 00:00:00 2001 From: hylom Date: Mon, 16 Mar 2009 19:35:35 +0900 Subject: [PATCH] add command: function, add id --- markup.py | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 159 insertions(+), 14 deletions(-) diff --git a/markup.py b/markup.py index 2b97e92..751d560 100755 --- 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 "\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 = """ - - +#
%s
-""" % (str_title) +# html = """ +# +# -
%s
+#""" % (anchor, str_title) + + add_anchor(anchor, str_title) + + html = """
+
%s
+
+""" % (anchor, str_title) print html def end_column(line): - print """
-""" + print """""" +# print """
+#""" def list_start(): return "
"
@@ -189,7 +203,7 @@ def fig(line, filehash):
 
 
 def table_start(cap):
-    return """
+    return """
""" % 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 '
%s
' + + 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'' % (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 '' % (href, str_command, href, str_desc) + + print "
%s:
%s%s
" + + + +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 "" + elif indenting < 1: + print '" + elif indenting < 5: + print '" + elif indenting < 10: + print '" + elif indenting > 1: + print "" + else : + print "" + + +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"

\1

", line) + if anchor != "": + add_anchor(anchor, line.strip()) + line = re.sub(ur"^●(.*)$", ur'

\1

' % anchor, line) + anchor = "" + else: + line = re.sub(ur"^●(.*)$", ur"

\1

", line) print line continue elif re.search(ur"^○", line): - line = re.sub(ur"^○(.*)$", ur"\1", line) + if anchor != "": + add_anchor(anchor, line.strip()) + line = re.sub(ur"^(.*)$", ur'\1
' % anchor, line) + anchor = "" + else: + line = re.sub(ur"^(.*)$", ur"\1
", line) print line continue elif re.search(ur"^☆----", line): line = re.sub(ur"☆----.*-{0,1}", u"
", 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") -- 2.11.0