Skip to content
Permalink
Browse files

Merge pull request #1666 from getnikola/wordpress-code-blocks

Wordpress code blocks (Fix #1186)
  • Loading branch information
ralsina committed May 22, 2015
2 parents 6bc0dd4 + 902fef7 commit 4d36bf82f70b47550392d99f2bee86ff30460e68
Showing with 39 additions and 22 deletions.
  1. +3 −0 CHANGES.txt
  2. +28 −11 nikola/plugins/command/import_wordpress.py
  3. +8 −11 tests/test_command_import_wordpress.py
@@ -4,6 +4,7 @@ New in master
Features
--------

* Support [code] in wordpress importers (Issue #1186)
* New `nikola status` command
* Cleaner formatting of HTML output
* Allowing category hierarchies via new option CATEGORY_ALLOW_HIERARCHIES
@@ -43,6 +44,7 @@ New in v7.4.0
Features
--------

>>>>>>> master
* Substitutions for RSS_LINKS_APPEND_QUERY for identifying
the source feed (feedRelUri) and the kind of feed (feedFormat).
* New option GENERATE_ATOM, off by default
@@ -60,6 +62,7 @@ Features
* Multilingual sitemaps (Issue #1610)
* Compatibility with doit v0.28.0 (Issue #1655)
* AddThis is no longer added by default to users’ sites
>>>>>>> master
* New translations (az, fil, tl, uk, zh_TW)
* Add reStructuredText transform support (Issue #1647)
* Produce Unicode output in ``nikola init`` (via Issue #1644)
@@ -335,13 +335,30 @@ def download_additional_image_sizes(self, item, wordpress_namespace, source_path
links[url] = '/' + dst_url
links[url] = '/' + dst_url

@staticmethod
def transform_sourcecode(content):
new_content = re.sub('\[sourcecode language="([^"]+)"\]',
"\n~~~~~~~~~~~~{.\\1}\n", content)
new_content = new_content.replace('[/sourcecode]',
"\n~~~~~~~~~~~~\n")
return new_content
code_re1 = re.compile(r'\[code.* lang.*?="(.*?)?".*\](.*?)\[/code\]', re.DOTALL | re.MULTILINE)
code_re2 = re.compile(r'\[sourcecode.* lang.*?="(.*?)?".*\](.*?)\[/sourcecode\]', re.DOTALL | re.MULTILINE)
code_re3 = re.compile(r'\[code.*?\](.*?)\[/code\]', re.DOTALL | re.MULTILINE)
code_re4 = re.compile(r'\[sourcecode.*?\](.*?)\[/sourcecode\]', re.DOTALL | re.MULTILINE)

def transform_code(self, content):
# http://en.support.wordpress.com/code/posting-source-code/. There are
# a ton of things not supported here. We only do a basic [code
# lang="x"] -> ```x translation, and remove quoted html entities (<,
# >, &, and ").
def replacement(m):
language = m.group(1) or ''
code = m.group(2)
code = code.replace('&amp;', '&')
code = code.replace('&gt;', '>')
code = code.replace('&lt;', '<')
code = code.replace('&quot;', '"')
return '```{language}\n{code}\n```'.format(language=language, code=code)

content = self.code_re1.sub(replacement, content)
content = self.code_re2.sub(replacement, content)
content = self.code_re3.sub(replacement, content)
content = self.code_re4.sub(replacement, content)
return content

@staticmethod
def transform_caption(content):
@@ -358,10 +375,10 @@ def transform_multiple_newlines(self, content):
return content

def transform_content(self, content):
new_content = self.transform_sourcecode(content)
new_content = self.transform_caption(new_content)
new_content = self.transform_multiple_newlines(new_content)
return new_content
content = self.transform_code(content)
content = self.transform_caption(content)
content = self.transform_multiple_newlines(content)
return content

def import_item(self, item, wordpress_namespace, out_folder=None):
"""Takes an item from the feed and creates a post file."""
@@ -228,13 +228,13 @@ def test_importing_posts_and_attachments(self):
Some source code.
~~~~~~~~~~~~{.Python}
```Python
import sys
print sys.version
~~~~~~~~~~~~
```
The end.
@@ -302,16 +302,16 @@ def test_importing_posts_and_attachments(self):

def test_transforming_content(self):
"""Applying markup conversions to content."""
transform_sourcecode = mock.MagicMock()
transform_code = mock.MagicMock()
transform_caption = mock.MagicMock()
transform_newlines = mock.MagicMock()

with mock.patch('nikola.plugins.command.import_wordpress.CommandImportWordpress.transform_sourcecode', transform_sourcecode):
with mock.patch('nikola.plugins.command.import_wordpress.CommandImportWordpress.transform_code', transform_code):
with mock.patch('nikola.plugins.command.import_wordpress.CommandImportWordpress.transform_caption', transform_caption):
with mock.patch('nikola.plugins.command.import_wordpress.CommandImportWordpress.transform_multiple_newlines', transform_newlines):
self.import_command.transform_content("random content")

self.assertTrue(transform_sourcecode.called)
self.assertTrue(transform_code.called)
self.assertTrue(transform_caption.called)
self.assertTrue(transform_newlines.called)

@@ -325,21 +325,18 @@ def test_transforming_source_code(self):
print sys.version
[/sourcecode]"""

content = self.import_command.transform_sourcecode(content)
content = self.import_command.transform_code(content)

self.assertFalse('[/sourcecode]' in content)
self.assertFalse('[sourcecode language=' in content)

replaced_content = """Hello World.
~~~~~~~~~~~~{.Python}
```Python
import sys
print sys.version
~~~~~~~~~~~~
"""

```"""
self.assertEqual(content, replaced_content)

def test_transform_caption(self):

0 comments on commit 4d36bf8

Please sign in to comment.
You can’t perform that action at this time.