OSDN Git Service

move defualt_markup_rules to each subrules.
[otptools/otptools.git] / markupper.py
index 0bdcf98..3de80fe 100755 (executable)
@@ -9,8 +9,8 @@ import pickle
 
 import HTMLTagFilter
 
-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 = ["*"]
@@ -39,6 +39,7 @@ class Markupper(object):
         self._input_iter = None
         self._index_past = {}
         self._index = {}
+        self._image_dir = ""
 
     def index_add(self, key, val):
         """
@@ -77,13 +78,16 @@ class Markupper(object):
 
         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"^☆comment\s{{{$", line):
                 self._comment(line)
                 continue
@@ -106,24 +110,13 @@ class Markupper(object):
                 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):
-                line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
-                print line
+                self._newpage(line)
                 continue
             elif re.search(ur"^☆\+---", line):
                 self._code(line)
@@ -137,17 +130,49 @@ class Markupper(object):
             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 _head_l(self, line):
+        line = self._default_markup_rule(line)
+        if self._anchor != "":
+            line = re.sub(ur"^●(.*)$", ur'<h4 id="%s">\1</h4>' % self._anchor, line)
+            self._anchor = ""
+        else:
+            line = re.sub(ur"^●(.*)$", ur"<h4>\1</h4>", line)
+        print line
+
+    def _head_m(self, line):
+        line = self._default_markup_rule(line)
+        if self._anchor != "":
+            line = re.sub(ur"^○(.*)$", ur'<b id="%s">\1</b>' % self._anchor, line)
+            self._anchor = ""
+        else:
+            line = re.sub(ur"^○(.*)$", ur"<b>\1</b>", 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):
         """
@@ -201,6 +226,12 @@ class Markupper(object):
     def _call_tagles(self):
         pass
 
+    def _escape(self, line):
+        line = re.sub(ur"&", ur"&amp", line)
+        line = re.sub(ur"<", ur"&lt;", line)
+        line = re.sub(ur">", ur"&gt;", line)
+        return line
+
     def _default_markup_rule(self, line):
         """
         apply default markup rules.
@@ -208,19 +239,18 @@ class Markupper(object):
         @param line: string to apply markup
         @type line: string
         """
-        # line = re.sub(ur"&", ur"&amp", line)
-        # line = re.sub(ur"<", ur"&lt;", line)
-        # line = re.sub(ur">", ur"&gt;", 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"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
         line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", 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):
@@ -233,6 +263,7 @@ class Markupper(object):
         """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"
@@ -263,18 +294,16 @@ class Markupper(object):
 
     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 self._list_start()
 
         for line in self.input_iter:
-            line = line.strip()
-            line = line.replace("&", "&amp;")
-            line = line.replace("<", "&lt;")
-            line = line.replace(">", "&gt;")
-            if line == "----":
+            line = line.strip("\n\r")
+            line = self._escape(line)
+            if re.search(ur"""^☆\+---""", line):
                 break
             print line
         print self._list_end()
@@ -283,10 +312,7 @@ class Markupper(object):
         print self._list_start()
 
         for line in self.input_iter:
-            #        line = line.strip()
-            line = line.replace("&", "&amp;")
-            line = line.replace("<", "&lt;")
-            line = line.replace(">", "&gt;")
+            line = self._escape(line)
             line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
             line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
 
@@ -295,14 +321,12 @@ class Markupper(object):
             print line,
         print self._list_end()
 
-
     def _inline(self, line):
         for line in self.input_iter:
             #        line = line.strip()
             if re.search(ur"^☆}}}", line):
                 break
-            print line
-
+            print line,
 
     def _comment(self, line):
         for line in self.input_iter:
@@ -313,6 +337,48 @@ class Markupper(object):
     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>
+
+"""
+
+        rex_title = re.compile(ur"^☆flow\s+(.*)$")
+        if rex_title.search(line):
+            title = rex_title.search(line).group(1)
+        else:
+            title = ""
+
+        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 = match.group(2)
+            else:
+                continue
+            fig = self._anchored_fig(file, cap)
+            outputs.append(flow_item % (fig, cap))
+
+        arrow = '<img src="%s">\n' % (down_arrow,)
+        print flow_header
+        print flow_title % (title,)
+        print arrow.join(outputs)
+        print flow_footer
+        
 
     def _fig_start(self, cap=""):
         return """<table align="center" border="0" cellpadding="0" cellspacing="0">
@@ -331,10 +397,10 @@ class Markupper(object):
         if self._release == 1:
             self._fig_release(line)
         else:
-            self._fig(line)
+            self._fig_org(line)
 
 
-    def _fig(self, line):
+    def _fig_org(self, line):
         try:
             str_title = re.search(ur"^☆(図.*)$", line).group(1)
         except AttributeError:
@@ -350,19 +416,22 @@ class Markupper(object):
         match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
         if not match_o1 == None:
             imgname = match_o1.group(1)
-            imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", 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_o1.group(1)
+            imgname = os.path.join(self._image_dir, imgname)
             imgname_s = match_o1.group(2)
 
-        if not os.path.isfile(imgname_s):
-            imgname_s = imgname
+        print self._anchored_fig(imgname, str_title, imgname_s)
 
-        print """<a href="%s">
-     <img src="%s" alt="%s">
-</a>
-     """ % (imgname, imgname_s, str_title)
-        
+#         if not os.path.isfile(imgname_s):
+#             imgname_s = imgname
+
+#         print """<a href="%s">
+#      <img src="%s" alt="%s">
+# </a>
+#      """ % (imgname, imgname_s, str_title)
 
         dic = self.index("figs")
         dic.append(imgname)
@@ -370,7 +439,20 @@ class Markupper(object):
             dic.append(imgname_s)
 
         print self._fig_end(str_title);
+        
+    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
+
+        return """<a href="%s">
+  <img src="%s" alt="%s">
+</a>
+""" % (file, file_s, alt)
+        
 
     def _fig_release(self, line):
         try:
@@ -414,12 +496,12 @@ class Markupper(object):
 
 
     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">
     <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 = ""
@@ -427,7 +509,7 @@ class Markupper(object):
 
         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 = ""
@@ -436,10 +518,15 @@ class Markupper(object):
 
         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
+            if re.search(ur"^※", line):
+                footnote = re.search(ur"^(※.*)$", line).group(1)
+                break
             line = self._default_markup_rule(line)
             if re.search(ur"^〓", line):
                 line = re.sub(ur"^〓", "", line)
@@ -496,7 +583,7 @@ class Markupper(object):
             # 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