Skip to content
Browse files

fixes for string2dict and dict2string

  • Loading branch information...
1 parent fa8d894 commit f7920246c0b09d6c5b7dd8da6a95914f6c92556c @jedie committed
View
17 creole/creole2html/emitter.py
@@ -16,8 +16,8 @@
import traceback
from creole.creole2html.parser import CreoleParser
-from creole.creole2html.str2dict import str2dict
-from creole.py3compat import TEXT_TYPE
+from creole.py3compat import TEXT_TYPE, repr2
+from creole.shared.utils import string2dict
class HtmlEmitter:
@@ -161,12 +161,12 @@ def macro_emit(self, node):
args = node.macro_args
try:
- macro_kwargs = str2dict(args)
+ macro_kwargs = string2dict(args)
except ValueError as e:
exc_info = sys.exc_info()
return self.error(
- "Wrong macro arguments: %r for macro '%s' (maybe wrong macro tag syntax?)" % (
- args, macro_name
+ "Wrong macro arguments: %s for macro '%s' (maybe wrong macro tag syntax?)" % (
+ repr2(args), macro_name
),
exc_info
)
@@ -287,7 +287,7 @@ def error(self, text, exc_info=None):
if __name__ == "__main__":
- txt = """This is __underlined__ text."""
+ txt = """A <<test_macro1 args="foo1">>bar1<</test_macro1>> in a line..."""
print("-" * 80)
# from creole_alt.creole import CreoleParser
@@ -295,7 +295,10 @@ def error(self, text, exc_info=None):
document = p.parse()
p.debug()
- html = HtmlEmitter(document).emit()
+ from creole.shared.unknown_tags import escape_unknown_nodes
+ html = HtmlEmitter(document,
+ macros=escape_unknown_nodes
+ ).emit()
print(html)
print("-" * 79)
print(html.replace(" ", ".").replace("\n", "\\n\n"))
View
12 creole/creole2html/parser.py
@@ -416,7 +416,7 @@ def _replace(self, match):
"""Invoke appropriate _*_repl method. Called for every matched group."""
# def debug(groups):
-# from pprint(import pformat)
+# from pprint import pformat
# data = dict([
# group for group in groups.items() if group[1] is not None
# ])
@@ -485,13 +485,7 @@ def debug_groups(self, groups):
print("-" * 80)
- txt = """one **line** and //jo//
-second line
-**third**
-
-block 2a
-block 2b
-block 2c"""
+ txt = """A <<test_macro1 args="foo1">>bar1<</test_macro1>> in a line..."""
print(txt)
print("-" * 80)
@@ -502,8 +496,6 @@ def debug_groups(self, groups):
document = p.parse()
p.debug()
- sys.exit()
-
def display_match(match):
groups = match.groupdict()
for name, text in groups.items():
View
22 creole/html2creole/emitter.py
@@ -150,22 +150,7 @@ def span_emit(self, node):
# import sys;sys.exit()
from creole.html_parser.parser import HtmlParser
- data = """
-<ul>
- <li><p>item 1</p>
- <ul>
- <li><p>subsubitem 1.1.1</p></li>
- <li>subsubitem 1.1.2</li>
- </ul>
- </li>
- <li><p>item 2</p>
- <ul>
- <li>subitem 2.1</li>
- </ul>
- </li>
-</ul>
-
-"""
+ data = """A <<test_macro1 args="foo1">>bar1<</test_macro1>> in a line..."""
# print(data.strip())
h2c = HtmlParser(
@@ -174,8 +159,11 @@ def span_emit(self, node):
document_tree = h2c.feed(data)
h2c.debug()
+ from creole.shared.unknown_tags import escape_unknown_nodes
+
e = CreoleEmitter(document_tree,
- debug=True
+ debug=True,
+ unknown_emit=escape_unknown_nodes
)
content = e.emit()
print("*" * 79)
View
11 creole/py3compat.py
@@ -15,9 +15,20 @@
# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3
+
if PY3:
TEXT_TYPE = str
BINARY_TYPE = bytes
else:
TEXT_TYPE = unicode
BINARY_TYPE = str
+
+
+def repr2(obj):
+ """
+ Don't mark unicode strings with u in Python 2
+ """
+ if not PY3:
+ return repr(obj).lstrip("u")
+ else:
+ return repr(obj)
View
9 creole/shared/document_tree.py
@@ -16,6 +16,7 @@
import inspect
from creole.py3compat import TEXT_TYPE
+from creole.shared.utils import dict2string
class DocNode:
@@ -53,13 +54,7 @@ def get_attrs_as_string(self):
>>> node.get_attrs_as_string()
'foo="bar" no="ABC"'
"""
- attr_list = []
- for key, value in self.attrs.items():
- if not isinstance(value, TEXT_TYPE):
- value = unicode(value)
- value_string = repr(value).lstrip("").replace("'", '"')
- attr_list.append("%s=%s" % (key, value_string))
- return " ".join(attr_list)
+ return dict2string(self.attrs)
def __str__(self):
return str(self.__repr__())
View
33 creole/creole2html/str2dict.py → creole/shared/utils.py
@@ -2,10 +2,8 @@
"""
- Creole Rules for parser
+ python creole utilities
~~~~~~~~~~~~~~~~~~~~~~~
-
- Helper take from PyLucid CMS project
:copyleft: 2011 by python-creole team, see AUTHORS for more details.
:license: GNU GPL v3 or above, see LICENSE for more details.
@@ -15,24 +13,24 @@
import shlex
-from creole.py3compat import TEXT_TYPE, PY3
+from creole.py3compat import TEXT_TYPE, PY3, repr2
-# For str2dict()
+# For string2dict()
KEYWORD_MAP = {
"True": True,
"False": False,
"None": None,
}
-def str2dict(raw_content, encoding="utf-8"):
+def string2dict(raw_content, encoding="utf-8"):
"""
convert a string into a dictionary. e.g.:
- >>> str2dict('key1="value1" key2="value2"')
+ >>> string2dict('key1="value1" key2="value2"')
{'key2': 'value2', 'key1': 'value1'}
- See test_creole2html.TestStr2Dict()
+ See test_creole2html.TestString2Dict()
"""
if not PY3 and isinstance(raw_content, TEXT_TYPE):
# shlex.split doesn't work with unicode?!?
@@ -59,6 +57,25 @@ def str2dict(raw_content, encoding="utf-8"):
return result
+def dict2string(d):
+ """
+ FIXME: Find a better was to do this.
+
+ >>> dict2string({'foo':"bar", "no":123})
+ 'foo="bar" no="123"'
+
+ >>> dict2string({"foo":'bar', "no":"ABC"})
+ 'foo="bar" no="ABC"'
+
+ See test_creole2html.TestDict2String()
+ """
+ attr_list = []
+ for key, value in sorted(d.items()):
+ value_string = repr2(value)
+ attr_list.append("%s=%s" % (key, value_string))
+ return " ".join(attr_list)
+
+
if __name__ == "__main__":
import doctest
print(doctest.testmod())
View
2 tests/run_all_tests.py
@@ -17,7 +17,7 @@
import unittest
import creole
-from tests.test_creole2html import TestCreole2html, TestCreole2htmlMarkup
+from tests.test_creole2html import TestCreole2html, TestCreole2htmlMarkup, TestStr2Dict, TestDict2String
from tests.test_cross_compare_all import CrossCompareTests
from tests.test_cross_compare_creole import CrossCompareCreoleTests
from tests.test_cross_compare_rest import CrossCompareReStTests
View
28 tests/test_creole2html.py
@@ -20,7 +20,7 @@
import sys
import unittest
-from creole.creole2html.str2dict import str2dict
+
try:
from StringIO import StringIO
except ImportError:
@@ -31,6 +31,7 @@
from creole import creole2html
from creole.shared import example_macros
+from creole.shared.utils import string2dict, dict2string
class TestCreole2html(unittest.TestCase):
@@ -549,28 +550,45 @@ def test_tt(self):
class TestStr2Dict(unittest.TestCase):
def test_basic(self):
self.assertEqual(
- str2dict('key1="value1" key2="value2"'),
+ string2dict('key1="value1" key2="value2"'),
{'key2': 'value2', 'key1': 'value1'}
)
def test_bool(self):
self.assertEqual(
- str2dict('unicode=True'),
+ string2dict('unicode=True'),
{'unicode': True}
)
def test_mixed1(self):
self.assertEqual(
- str2dict('A="B" C=1 D=1.1 E=True F=False G=None'),
+ string2dict('A="B" C=1 D=1.1 E=True F=False G=None'),
{'A': 'B', 'C': 1, 'E': True, 'D': '1.1', 'G': None, 'F': False}
)
def test_mixed2(self):
self.assertEqual(
- str2dict('''key1="'1'" key2='"2"' key3="""'3'""" '''),
+ string2dict('''key1="'1'" key2='"2"' key3="""'3'""" '''),
{'key3': 3, 'key2': 2, 'key1': 1}
)
+class TestDict2String(unittest.TestCase):
+ def test_basic(self):
+ self.assertEqual(
+ dict2string({'key':'value'}),
+ "key='value'"
+ )
+
+ def test_basic2(self):
+ self.assertEqual(
+ dict2string({'foo':"bar", "no":123}),
+ "foo='bar' no=123"
+ )
+ def test_basic3(self):
+ self.assertEqual(
+ dict2string({"foo":'bar', "no":"ABC"}),
+ "foo='bar' no='ABC'"
+ )
if __name__ == '__main__':
unittest.main(
View
4 tests/test_cross_compare_creole.py
@@ -419,12 +419,12 @@ def test_html_macro_unknown_nodes(self):
"""
self.cross_compare_creole("""
111 <<html>><x><</html>>foo<<html>></x><</html>> 222
- 333<<html>><x foo1="bar1"><</html>>foobar<<html>></x><</html>>444
+ 333<<html>><x foo1='bar1'><</html>>foobar<<html>></x><</html>>444
555<<html>><x /><</html>>666
""", """
<p>111 <x>foo</x> 222<br />
- 333<x foo1="bar1">foobar</x>444</p>
+ 333<x foo1='bar1'>foobar</x>444</p>
<p>555<x />666</p>
""",
View
6 tests/test_html2creole.py
@@ -76,7 +76,7 @@ def test_use_html_macro(self):
"""
self.assert_html2creole(r"""
111 <<html>><unknown><</html>>foo<<html>></unknown><</html>> 222
- 333<<html>><unknown foo1="bar1" foo2="bar2"><</html>>foobar<<html>></unknown><</html>>444
+ 333<<html>><unknown foo1='bar1' foo2='bar2'><</html>>foobar<<html>></unknown><</html>>444
555<<html>><unknown /><</html>>666
""", """
@@ -95,12 +95,12 @@ def test_escape_unknown_nodes(self):
"""
self.assert_html2creole(r"""
111 &lt;unknown&gt;foo&lt;/unknown&gt; 222
- 333&lt;unknown foo1="bar1" foo2="bar2"&gt;foobar&lt;/unknown&gt;444
+ 333&lt;unknown foo1='bar1' foo2='bar2'&gt;foobar&lt;/unknown&gt;444
555&lt;unknown /&gt;666
""", """
<p>111 <unknown>foo</unknown> 222<br />
- 333<unknown foo1="bar1" foo2="bar2">foobar</unknown>444</p>
+ 333<unknown foo1="bar1" foo2='bar2'>foobar</unknown>444</p>
<p>555<unknown />666</p>
""",
View
7 tests/test_macros.py
@@ -11,8 +11,12 @@
:license: GNU GPL v3 or above, see LICENSE for more details.
"""
+
from __future__ import division, absolute_import, print_function, unicode_literals
+from creole.py3compat import repr2
+
+
def test_macro1(**kwargs):
"""
>>> test_macro1(foo="bar")
@@ -24,9 +28,10 @@ def test_macro1(**kwargs):
>>> test_macro1(a=1,b=2)
'[test macro1 - kwargs: a=1,b=2]'
"""
- kwargs = ','.join(['%s=%r' % (k, v) for k, v in sorted(kwargs.items())])
+ kwargs = ','.join(['%s=%s' % (k, repr2(v)) for k, v in sorted(kwargs.items())])
return "[test macro1 - kwargs: %s]" % kwargs
+
def test_macro2(char, text):
"""
>>> test_macro2(char="|", text="a\\nb")

0 comments on commit f792024

Please sign in to comment.
Something went wrong with that request. Please try again.