Commits

Naoya Inada committed 22ed574

Add directive for Hatena notation "wikipedia"

  • Participants
  • Parent commits 7fd1d64

Comments (0)

Files changed (7)

File examples/sample.rst

 Hatena "amazon" notation.
 
 .. amazon:: Python
+
+
+Hatena "wikipedia" notation.
+
+.. wikipedia:: Python
+
+.. wikipedia:: Python
+    :lang: en

File rst2hatena/directives/__init__.py

 # -*- coding:utf-8 -*-
 
 # First Created: 2011-07-03 18:30:13 +0900
-# Last Modified: 2011-07-05 13:08:11 +0900
+# Last Modified: 2011-07-05 22:14:00 +0900
 
 # Copyright (c) 2011 Naoya INADA <naoina@naniyueni.org>
 # All rights reserved.
     "niconico": ("rst2hatena", "Niconico"),
     "google": ("rst2hatena", "Google"),
     "amazon": ("rst2hatena", "Amazon"),
+    "wikipedia": ("rst2hatena", "Wikipedia"),
 }
 
 _hatena_directives = {
     "niconico": "niconico",
     "google": "google",
     "amazon": "amazon",
+    "wikipedia": "wikipedia",
 }
 
 directives._directive_registry.update(_hatena_directive_registry)

File rst2hatena/directives/autolink.py

 # -*- coding:utf-8 -*-
 
 # First Created: 2011-07-05 13:04:10 +0900
-# Last Modified: 2011-07-05 21:32:53 +0900
+# Last Modified: 2011-07-05 22:15:43 +0900
 
 # Copyright (c) 2011 Naoya INADA <naoina@naniyueni.org>
 # All rights reserved.
     "Niconico",
     "Google",
     "Amazon",
+    "Wikipedia",
 ]
 
 import re
                     '"target" are: "%s".'
                     % (self.name, self.options["target"],
                        '", "'.join(self.target_values)))
-        self.options["query"] = directives.unchanged_required(self.arguments[0])
+        self.options["query"] = directives.unchanged_required(
+                self.arguments[0])
         google_node = hatena_nodes.google(self.block_text, **self.options)
         return [google_node]
 
         return [amazon_node]
 
 
+class Wikipedia(Directive):
+    required_arguments = 1
+    optional_arguments = 0
+    final_argument_whitespace = True
+    option_spec = {
+        "lang": directives.unchanged,
+    }
+
+    def run(self):
+        self.options["query"] = directives.unchanged_required(
+                self.arguments[0])
+        wikipedia_node = hatena_nodes.wikipedia(self.block_text,
+                **self.options)
+        return [wikipedia_node]
+
+
 def main():
     pass
 

File rst2hatena/hatena_nodes.py

 # -*- coding:utf-8 -*-
 
 # First Created: 2011-07-03 18:01:09 +0900
-# Last Modified: 2011-07-05 13:00:55 +0900
+# Last Modified: 2011-07-05 22:09:52 +0900
 
 # Copyright (c) 2011 Naoya INADA <naoina@naniyueni.org>
 # All rights reserved.
     "niconico",
     "google",
     "amazon",
+    "wikipedia",
         ]
 
 from docutils.nodes import General, Inline, Element
 
+
 class niconico(General, Element): pass
 class google(General, Inline, Element): pass
 class amazon(General, Inline, Element): pass
+class wikipedia(General, Inline, Element): pass
 
 
 def main():

File rst2hatena/hatena_writer/__init__.py

             self.body.append('>>')
         self.body.append('\n')
         self.body.append('    ' * (self.block_quote_level - 1))
-        
+
     def depart_block_quote(self, node):
         self.block_quote_level -= 1
         if self.block_quote_level == 0:
         self.body.append('')  # dummy for replace later
         self.body.append('))')
         raise nodes.SkipNode
-    
+
     def depart_footnote_reference(self, node):
         pass
 
         if not node.has_key('refid'):
             return
         self.in_citation_key = node['refid']
-        
+
     def depart_citation_reference(self, node):
         self.body.append('((')
         if not self.citation_idx_dict.has_key(self.in_citation_key):
     def depart_amazon(self, node):
         if not self.in_paragraph:
             self.body.append("\n")
+
+    def visit_wikipedia(self, node):
+        lang = node["lang"] + ":" if "lang" in node else ""
+        self.body.append("[wikipedia:%s]" % (lang + node["query"]))
+
+    def depart_wikipedia(self, node):
+        if not self.in_paragraph:
+            self.body.append("\n")

File rst2hatena/hatena_writer/base_translator.py

 
     def depart_amazon(self, node):
         raise NotImplementedError('"amazon" directive is not implemented')
+
+    def visit_wikipedia(self, node):
+        raise NotImplementedError('"wikipedia" directive is not implemented')
+
+    def depart_wikipedia(self, node):
+        raise NotImplementedError('"wikipedia" directive is not implemented')

File rst2hatena/tests/test_writer.py

         self.assertEqual(exp_str, output)
 
 
+    def test_wikipedia_directive(self):
+        str = """\
+Hatena "wikipedia" notation.
+
+.. wikipedia:: Python
+
+.. wikipedia:: Python
+    :lang: en
+"""
+        exp_str = """\
+Hatena "wikipedia" notation.
+
+[wikipedia:Python]
+[wikipedia:en:Python]
+"""
+        output = publish_string(str, writer=self.writer)
+        self.assertEqual(exp_str, output)
+
+
 if __name__ == '__main__':
     suite = unittest.TestLoader().loadTestsFromTestCase(WriterTest)
     try: