OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / markupper.py
old mode 100755 (executable)
new mode 100644 (file)
index 945e8d3..bfdcdc6
@@ -9,6 +9,7 @@ import pickle
 
 import HTMLTagFilter
 import deterfile
+import getjpggeom
 
 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
@@ -72,6 +73,7 @@ class Markupper(object):
         """
         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)
@@ -89,6 +91,9 @@ class Markupper(object):
             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
@@ -110,6 +115,12 @@ class Markupper(object):
             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
@@ -122,6 +133,9 @@ class Markupper(object):
             elif re.search(ur"^○", line):
                 self._head_m(line)
                 continue
+            elif re.search(ur"^◇", line):
+                self._head_s(line)
+                continue
             elif re.search(ur"^☆----", line):
                 self._newpage(line)
                 continue
@@ -134,9 +148,15 @@ class Markupper(object):
             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
@@ -186,6 +206,20 @@ class Markupper(object):
             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>"
@@ -248,7 +282,7 @@ class Markupper(object):
         pass
 
     def _escape(self, line):
-        line = re.sub(ur"&", ur"&amp", line)
+        line = re.sub(ur"&", ur"&amp;", line)
         line = re.sub(ur"<", ur"&lt;", line)
         line = re.sub(ur">", ur"&gt;", line)
         return line
@@ -265,11 +299,17 @@ class Markupper(object):
         # 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)
 
@@ -299,7 +339,6 @@ class Markupper(object):
         print "</ol>\n"
 
 
-
     def _begin_column(self, line):
         """Proccess column"""
         try:
@@ -307,15 +346,34 @@ class Markupper(object):
         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):
@@ -326,15 +384,19 @@ class Markupper(object):
 
     def _list(self, line):
         try:
-            str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
+            str_title = re.search(ur"^☆((リスト|実行例).*)$", line).group(1)
         except AttributeError:
             str_title = ""
-        print "<p><b>%s</b></p>" % (str_title)
+        print "<p class='caption'><b>%s</b></p>" % (str_title)
         print self._list_start()
 
         for line in self.input_iter:
             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
@@ -345,8 +407,10 @@ class Markupper(object):
 
         for line in self.input_iter:
             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)
+            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
@@ -355,7 +419,7 @@ class Markupper(object):
 
     def _inline(self, line):
         for line in self.input_iter:
-            #        line = line.strip()
+            line = line.strip()
             if re.search(ur"^☆}}}", line):
                 break
             print line
@@ -439,19 +503,20 @@ class Markupper(object):
     </table>
     """ % (cap)
 
-    def _fig(self, line):
-        if self._release == 1:
-            self._fig_release(line)
+    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:
-            self._fig(line)
+            desc = deterfile.file(filepath)
 
-    def _get_png_geom(self, filepath):
-        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))
+            err = ", ".join(desc)
+            raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
         if m:
             w = m.group(1)
             h = m.group(2)
@@ -486,6 +551,56 @@ class Markupper(object):
             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:
@@ -513,10 +628,13 @@ class Markupper(object):
 
         alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
 
-        return """<a href="%s">
+        ret = """<a href="%s">
   <img src="%s" alt="%s">
 </a>
 """ % (file, file_s, alt)
+
+
+        return ret
         
 
     def _fig_release(self, line):
@@ -561,7 +679,7 @@ class Markupper(object):
 
 
     def _table_start(self, cap):
-        return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table">
+        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
 
@@ -595,7 +713,6 @@ class Markupper(object):
             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)
                 tag_mode = "th"
@@ -621,8 +738,21 @@ class Markupper(object):
                             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