← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/fix_code_in_blockquotes into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/fix_code_in_blockquotes into lp:widelands-website.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/fix_code_in_blockquotes/+merge/332966

Something that annoys me a long time: When quoting a post which contains a code block, the codeblock is not rendered correct. This branch will fix this.

The approach is to exchange the syntax for quoted code and use the normal markdown syntax for this. E.g. a quoted codeblock looks like this:

> ~~~~
> code line 1
> code line 2
> ~~~~

This will be changed into:

>     code line 1
>     code line 2

The line with tildes ('~') get removed and all lines between get additionally 4 spaces at the beginning. After markdown runs over this, the code block will be rendered correctly.

I am not happy with my code, but right now i don't know how to do it better. I tried also using regular expressions, but i didn't get it to work. Suggestions welcome :-)
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/fix_code_in_blockquotes into lp:widelands-website.
=== modified file 'mainpage/templatetags/wl_markdown.py'
--- mainpage/templatetags/wl_markdown.py	2017-07-06 06:28:44 +0000
+++ mainpage/templatetags/wl_markdown.py	2017-10-29 19:29:00 +0000
@@ -156,15 +156,55 @@
     return None
 
 
+def _replace_code_tag(text):
+    """Replaces syntax for code in blockquotes. The syntax with tilde (~) is
+    derived from Fenced Codeblock extension, which could not be used in
+    blockquotes. Instead we use original mardown syntax (4 spaces) E.g.:
+
+    > ~~~~
+    > code snippet
+    > ~~~~
+
+    get replaced with:
+
+    >     code snippet
+
+    """
+
+    lines = text.split('\n')
+    is_code = False
+    new_text = []
+    for line in lines:
+        if line.startswith('> ~~~'):
+            # A syntax for code is found in a blockquote
+            # is_code gets True as long no such second line is found 
+            if is_code == True:
+                # Second line found
+                is_code = False
+            else:
+                is_code = True
+            continue
+        if is_code:
+            # Replace text of code with normal markup syntax (4 spaces)
+            new_text.append(line.replace('> ', '>     ', 1))
+        else:
+            # Not quoted code
+            new_text.append(line)
+
+    return '\n'.join(new_text)
+
+
 # Predefine the markdown extensions here to have a clean code in
 # do_wl_markdown()
 md_extensions = ['extra', 'toc', SemanticWikiLinkExtension()]
 
+
 def do_wl_markdown(value, *args, **keyw):
     # Do Preescaping for markdown, so that some things stay intact
     # This is currently only needed for this smiley ">:-)"
     value = _insert_smiley_preescaping(value)
     custom = keyw.pop('custom', True)
+    value = _replace_code_tag(value)
     html = smart_str(markdown(value, extensions=md_extensions))
 
     # Sanitize posts from potencial untrusted users (Forum/Wiki/Maps)


Follow ups