James Rivett-Carnac avatar James Rivett-Carnac committed f9cc0a8

code hilighting works for markdown and rest, though the code generated is different, which is annoying. textile has not been tested, because i never use it

Comments (0)

Files changed (3)

diario/tests/utils.py

 
 from django.test import TestCase
 from django.test.utils import override_settings
-from diario.utils import rst_helper
+from diario.utils import rst_helper, markuping
 
 BASIC_RST = u"""
 This is a Title
 
 a paragraph"""
 
-BASIC_HTML = u"""
+BASIC_MD = u"""
+# This is a Title
+
+ - A List
+ - with 2 items
+
+a paragraph"""
+
+BASIC_TXTL = """
+h1. This is a Title
+
+* A List
+* with 2 items
+
+a paragraph"""
+
+BASIC_RST_HTML = u"""
 <div class="document" id="this-is-a-title">
 <h1 class="title">This is a Title</h1>
 <ul class="simple">
 <p>a paragraph</p>
 </div>"""
 
+BASIC_HTML = u"""
+<h1>This is a Title</h1>
+<ul>
+  <li>A List</li>
+  <li>with 2 items</li>
+</ul>
+<p>a paragraph</p>"""
+
 #Now for that with sourcode
 CODE_RST = u"""
 This is a Title
 
 a paragraph"""
 
+CODE_MD = u"""
+# This is a Title
+
+    #!/usr/bin/env python
+    print "Hello World!"
+
+a paragraph"""
+
+CODE_TXTL = u"""
+h1. This is a Title
+
+bc. #!/usr/bin/env python
+print "Hello World!"
+
+a paragraph"""
+
 CODE_HTML = u"""
 <div class="document" id="this-is-a-title">
 <h1 class="title">This is a Title</h1>
 <p>a paragraph</p>
 </div>"""
 
-from django.conf import settings
+CODE_MD_HTML = u"""
+<h1>This is a Title</h1>
+<table class="syntaxtable"><tr><td class="linenos"><div class="linenodiv"><pre>1
+2</pre></div></td><td class="code"><div class="syntax"><pre><span class="c">#!/usr/bin/env python</span>
+<span class="k">print</span> <span class="s">&quot;Hello World!&quot;</span>
+</pre></div>
+</td></tr></table>
 
-class TestRst(TestCase):
+<p>a paragraph</p>"""
+
+class TestMarkup(TestCase):
     """Test generic reStructuredText and with the `sourcode` directive"""
     @override_settings(DIARIO_USE_COLOURS=False)
-    def test_basic(self):
-        self.assertHTMLEqual(rst_helper.html_body(BASIC_RST),BASIC_HTML)
+    def test_rest_helper_basic(self):
+        self.assertHTMLEqual(rst_helper.html_body(BASIC_RST),BASIC_RST_HTML)
 
     @override_settings(DIARIO_USE_COLOURS=True)
-    def test_lexical_higlighting(self):
-        settings.DIARIO_USE_COLOURS = True
+    def test_rest_helper_code(self):
         self.assertHTMLEqual(rst_helper.html_body(CODE_RST),CODE_HTML)
+
+    # Test Basic markdown.
+    @override_settings(DIARIO_USE_COLOURS=False,msg='md basic')
+    def test_markdown_basic(self):
+        self.assertHTMLEqual(markuping('markdown',BASIC_MD),BASIC_HTML)
+
+    @override_settings(DIARIO_USE_COLOURS=False,msg='rst basic')
+    def test_rest_basic(self):
+        self.assertHTMLEqual(markuping('rest',BASIC_RST),BASIC_RST_HTML)
+
+#    @override_settings(DIARIO_USE_COLOURS=False,msg='textile basic')
+#    def test_textile_basic(self):
+#        self.assertHTMLEqual(markuping('textile',BASIC_TXTL),BASIC_HTML)
+
+    # Test Markdow with lexical hilighting.
+    @override_settings(DIARIO_USE_COLOURS=True,msg='md code')
+    def test_markdown_code(self):
+        self.assertHTMLEqual(markuping('markdown',CODE_MD),CODE_MD_HTML)
+
+    @override_settings(DIARIO_USE_COLOURS=True)
+    def test_rest_code(self):
+        self.assertHTMLEqual(markuping('rest',CODE_RST),CODE_HTML)
+
+#    @skipIf(True)
+#    @override_settings(DIARIO_USE_COLOURS=True)
+#    def test_textile_code(self):
+#        self.assertHTMLEqual(markuping('textile',CODE_TXTL),CODE_HTML)

diario/utils/markup.py

             # There is now a question on if there is a 'code' block inside
             # the pre block
             bottom_node = node.code
-            if not code_node:
+            if not node.code:
                 bottom_node = node
             if not lexer_name or (lexer_name not in  _lexer_names):
                 lexer = guess_lexer(node.renderContents())
     if markup == 'markdown':
         from markdown import markdown
         if DIARIO_USE_COLOURS:
-            return markdown.markdown(value,
+            return markdown(value,
                     ['codehilite(css_class={0})'.format(DIARIO_COLOUR_CLASS)]
                     )
         output = markdown(value)

diario/utils/rst_helper.py

 
 # Set to True if you want inline CSS styles instead of classes, but do it in
 # django project settings
+from django.conf import settings
+from django.utils.encoding import smart_str, force_unicode
+from django.utils.safestring import mark_safe
+
 from diario.settings import DIARIO_RST_INLINESTYLES
 
 from pygments.formatters import HtmlFormatter
 def html_body(input_string,source_path=None,destination_path=None):
     """ Given a string input, get the contents of <body>...</body> w/o
     the body tags"""
-    parts = core.publish_parts(input_string,
-            source_path=source_path,
-            destination_path=destination_path,
-            writer_name='html')
-    return parts['html_body']
+    docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
+    parts = core.publish_parts(source=smart_str(input_string), writer_name="html4css1", settings_overrides=docutils_settings)
+    return mark_safe(force_unicode(parts["html_body"]))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.