OSDN Git Service

change markup.py's options: md5file isn't require.
[otptools/otptools.git] / markupper.py
index dd87617..0bdcf98 100755 (executable)
@@ -36,9 +36,9 @@ class Markupper(object):
     """
     """
     def __init__(self):
-        self.input_iter = None
-        self.index_past = {}
-        self.index = {}
+        self._input_iter = None
+        self._index_past = {}
+        self._index = {}
 
     def index_add(self, key, val):
         """
@@ -50,9 +50,18 @@ class Markupper(object):
         @param val:
         @key val:
         """
-        self.index[key] = val
+        self._index[key] = val
 
-    def markup(self, input_iter):
+    def index(self, key):
+        """
+        Get index
+        """
+        return self._index[key]
+
+    def index_haskey(self, key):
+        return self._index.has_key(key)
+
+    def markup(self, input_iter, release="0"):
         """
         Do markup.
 
@@ -64,6 +73,9 @@ class Markupper(object):
         # 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 = ""
         for line in self.input_iter:
@@ -90,6 +102,9 @@ class Markupper(object):
             elif re.search(ur"^☆space", line):
                 self._space(line)
                 continue
+            elif re.search(ur"^☆call_tables", line):
+                self._call_tables(line)
+                continue
             elif re.search(ur"^●", line):
                 if anchor != "":
                     line = re.sub(ur"^●(.*)$", ur'<h4 id="%s">\1</h4>' % anchor, line)
@@ -144,7 +159,7 @@ class Markupper(object):
         # load index
         try:
             index_file = open(path_to_index, "r")
-            self.index_past = pickle.load(index_file)
+            self._index_past = pickle.load(index_file)
             index_file.close()
         except IOError:
             sys.stderr.write("warn: cannot read index file,\n")
@@ -159,7 +174,7 @@ class Markupper(object):
         # save index
         try:
             index_file = open(path_to_index, "w")
-            pickle.dump(self.index, index_file)
+            pickle.dump(self._index, index_file)
             index_file.close()
         except IOError:
             sys.stderr.write("warn: cannot write index file,\n")
@@ -183,6 +198,8 @@ class Markupper(object):
             # hashlist's format: <hash> \t <filename>
             self.hashlist[splited[1]] = splited[0]
 
+    def _call_tagles(self):
+        pass
 
     def _default_markup_rule(self, line):
         """
@@ -202,6 +219,7 @@ class Markupper(object):
         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)
 
         # comment
@@ -269,7 +287,8 @@ class Markupper(object):
             line = line.replace("&", "&amp;")
             line = line.replace("<", "&lt;")
             line = line.replace(">", "&gt;")
-            line = line = re.sub(ur"★b\[(.*?)]", ur"<b>\1</b>", line)
+            line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
+            line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
 
             if re.search(ur"^☆\+---$", line):
                 break
@@ -309,6 +328,51 @@ class Markupper(object):
     """ % (cap)
 
     def _fig(self, line):
+        if self._release == 1:
+            self._fig_release(line)
+        else:
+            self._fig(line)
+
+
+    def _fig(self, line):
+        try:
+            str_title = re.search(ur"^☆(図.*)$", line).group(1)
+        except AttributeError:
+            str_title = ""
+        print self._fig_start()
+
+        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_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
+        elif not match_o2 == None:
+            imgname = match_o1.group(1)
+            imgname_s = match_o1.group(2)
+
+        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)
+        if imgname_s != "":
+            dic.append(imgname_s)
+
+        print self._fig_end(str_title);
+
+
+    def _fig_release(self, line):
         try:
             str_title = re.search(ur"^☆(図.*)$", line).group(1)
         except AttributeError:
@@ -316,24 +380,35 @@ class Markupper(object):
         print self._fig_start()
 
         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_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
-            hash = self.hashlist.get(match_o1.group(1), "")
-            hash_s = self.hashlist.get(imgname_s, "")
-            if hash_s == "":
-                hash_s = self.hashlist.get(match_o1.group(1), "")
         elif not match_o2 == None:
-            hash = self.hashlist.get(match_o2.group(1), "")
-            hash_s = self.hashlist.get(match_o2.group(2), "")
+            imgname = match_o1.group(1)
+            imgname_s = match_o1.group(2)
+
+
+        hash = self.hashlist.get(imgname, "")
+        hash_s = self.hashlist.get(imgname_s, "")
+        if hash_s == "":
+            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)
+        if imgname_s != "":
+            dic.append(imgname_s)
 
         print self._fig_end(str_title);
 
@@ -348,11 +423,16 @@ class Markupper(object):
 
     def _table(self, line):
         str_title = ""
+        self._table_buf1 = ""
+
         try:
             str_title = re.search(ur"^☆(表.*)$", line).group(1)
+            fig_name =  re.search(ur"^☆(表[0-9A-Z]+)", line).group(1)
         except AttributeError:
             str_title = ""
-        print _self.table_start(str_title)
+            fig_name = ""
+        print self._table_start(str_title)
+        self._table_buf1 =  self._table_start(str_title)
 
         num_row = 0
         table_contents = []
@@ -360,7 +440,7 @@ class Markupper(object):
             line = line.strip(" \n")
             if re.search(ur"^\s*$", line):
                 break
-            line = default_markup_rule(line)
+            line = self._default_markup_rule(line)
             if re.search(ur"^〓", line):
                 line = re.sub(ur"^〓", "", line)
                 tag_mode = "th"
@@ -406,6 +486,7 @@ class Markupper(object):
                 line = line + "</" + item["tag"] + ">"
             line = line + "</tr>\n"
             print line,
+            self._table_buf1 = self._table_buf1 + line
 
             # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
             # line = line.replace("\t", "</th><th>")
@@ -416,4 +497,15 @@ class Markupper(object):
             # print line
 
         print self._table_end()
+        self._table_buf1 =  self._table_buf1 + self._table_end()
+        if self.index_haskey("tables"):
+            self.index("tables")[fig_name] = self._table_buf1
+        else:
+            self.index_add("tables", {fig_name:self._table_buf1})
 
+    def _call_tables(self, line):
+        try:
+            fig_name =  re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
+        except AttributeError:
+            return
+        print self.index("tables")[fig_name]