diff options
author | JensDiemer <git@jensdiemer.de> | 2022-08-20 23:15:27 +0200 |
---|---|---|
committer | JensDiemer <git@jensdiemer.de> | 2022-08-20 23:16:13 +0200 |
commit | bf4afc69adc3cb2e92d3eddac8fa764c5d5ef741 (patch) | |
tree | b5c422e476c1dec3eb3cad48229013a659bf3b86 | |
parent | e5d71d823a4c304ef3c105feb6d08222ae985bf7 (diff) | |
download | creole-bf4afc69adc3cb2e92d3eddac8fa764c5d5ef741.tar.gz |
Fix Markdown tables + expand demo.py + update README
Add demo.py output into wiki, here:
https://github.com/jedie/python-creole/wiki
-rw-r--r-- | README.creole | 21 | ||||
-rw-r--r-- | README.md | 25 | ||||
-rw-r--r-- | README.rst | 26 | ||||
-rw-r--r-- | creole/emitter/html2markdown_emitter.py | 26 | ||||
-rw-r--r-- | creole/shared/markup_table.py | 17 | ||||
-rw-r--r-- | creole/tests/test_html2markdown.py | 45 | ||||
-rw-r--r-- | demo.py | 10 |
7 files changed, 116 insertions, 54 deletions
diff --git a/README.creole b/README.creole index 3c3cf99..d001f97 100644 --- a/README.creole +++ b/README.creole @@ -200,11 +200,6 @@ Creole Markup Cheat Sheet can be found here: http://www.wikicreole.org/wiki/Chea # Run pytest via tox with all environments: ~/python-creole$ make tox - -# Run pytest via tox with one Python version: -~/python-creole$ make tox-py38 -~/python-creole$ make tox-py37 -~/python-creole$ make tox-py36 }}} == make targets == @@ -215,16 +210,13 @@ To see all make targets, just call {{{make}}}: help List all commands install-poetry install or update poetry install install python-creole via poetry +update Update the dependencies as according to the pyproject.toml file lint Run code formatters and linter fix-code-style Fix code formatting tox-listenvs List all tox test environments tox Run pytest via tox with all environments -tox-py36 Run pytest via tox with *python v3.6* -tox-py37 Run pytest via tox with *python v3.7* -tox-py38 Run pytest via tox with *python v3.8* -tox-py39 Run pytest via tox with *python v3.9* pytest Run pytest -update-rst-readme update README.rst from README.creole +update-readmes update README.rst from README.creole publish Release new version to PyPi }}} @@ -238,20 +230,21 @@ Note: In this case you must install **docutils**! See above. = history = -* *dev* - [[https://github.com/jedie/python-creole/compare/v1.4.10...master|compare v1.4.10...master]] +* [[https://github.com/jedie/python-creole/compare/v1.5.0...master|*dev*]] +** TBC +* [[https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0|v1.5.0 - 2022-08-20]] ** NEW: html2markdown ** creole2html bugfixes: *** replace wrong {{{<tt>}}} with {{{<code>}}} *** Add newline after lists ** Remove deprecated "parser_kwargs" and "emitter_kwargs" ** Rename git {{{master}}} branch to {{{main}}}. -** TBC -* v1.4.10 - 2021-05-11 - [[https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10|compare v1.4.9...v1.4.10]] +* [[https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10|v1.4.10 - 2021-05-11]] ** Update some string formatting to f-strings ** Replace some {{{join()}}} list comprehension with generators ** Test on github actions also under MacOS ** Remove Travis CI (All tests already running via github actions) -* v1.4.9 - 2020-11-4 - [[https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9|compare v1.4.8...v1.4.9]] +* [[https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9|v1.4.9 - 2020-11-4]] ** Add missing classifier for Python 3.9 ([[https://github.com/jedie/python-creole/pull/55|Contributed by jugmac00]]) ** Update readme test * v1.4.8 - 2020-10-17 - [[https://github.com/jedie/python-creole/compare/v1.4.7...v1.4.8|compare v1.4.7...v1.4.8]] @@ -24,6 +24,7 @@ The creole2html part based on the creole markup parser and emitter from the Moin |  | |  | |  | + # install Python packages available on: [http://pypi.python.org/pypi/python-creole/](http://pypi.python.org/pypi/python-creole/) @@ -206,11 +207,6 @@ Creole Markup Cheat Sheet can be found here: [http://www.wikicreole.org/wiki/Che # Run pytest via tox with all environments: ~/python-creole$ make tox - -# Run pytest via tox with one Python version: -~/python-creole$ make tox-py38 -~/python-creole$ make tox-py37 -~/python-creole$ make tox-py36 ``` ## make targets @@ -221,16 +217,13 @@ To see all make targets, just call `make`: help List all commands install-poetry install or update poetry install install python-creole via poetry +update Update the dependencies as according to the pyproject.toml file lint Run code formatters and linter fix-code-style Fix code formatting tox-listenvs List all tox test environments tox Run pytest via tox with all environments -tox-py36 Run pytest via tox with *python v3.6* -tox-py37 Run pytest via tox with *python v3.7* -tox-py38 Run pytest via tox with *python v3.8* -tox-py39 Run pytest via tox with *python v3.9* pytest Run pytest -update-rst-readme update README.rst from README.creole +update-readmes update README.rst from README.creole publish Release new version to PyPi ``` ## Use creole in README @@ -243,20 +236,21 @@ Note: In this case you must install **docutils**! See above. # history -* *dev* - [compare v1.4.10...master](https://github.com/jedie/python-creole/compare/v1.4.10...master) +* [*dev*](https://github.com/jedie/python-creole/compare/v1.5.0...master) + * TBC +* [v1.5.0 - 2022-08-20](https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0) * NEW: html2markdown * creole2html bugfixes: * replace wrong `<tt>` with `<code>` * Add newline after lists * Remove deprecated "parser_kwargs" and "emitter_kwargs" * Rename git `master` branch to `main`. - * TBC -* v1.4.10 - 2021-05-11 - [compare v1.4.9...v1.4.10](https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10) +* [v1.4.10 - 2021-05-11](https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10) * Update some string formatting to f-strings * Replace some `join()` list comprehension with generators * Test on github actions also under MacOS * Remove Travis CI (All tests already running via github actions) -* v1.4.9 - 2020-11-4 - [compare v1.4.8...v1.4.9](https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9) +* [v1.4.9 - 2020-11-4](https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9) * Add missing classifier for Python 3.9 ([Contributed by jugmac00](https://github.com/jedie/python-creole/pull/55)) * Update readme test * v1.4.8 - 2020-10-17 - [compare v1.4.7...v1.4.8](https://github.com/jedie/python-creole/compare/v1.4.7...v1.4.8) @@ -444,6 +438,7 @@ first source code was written 27.11.2008: [Forum thread (de)](http://www.python- | GitHub | [https://github.com/jedie/python-creole](https://github.com/jedie/python-creole) | | Wiki | [https://github.com/jedie/python-creole/wiki](https://github.com/jedie/python-creole/wiki) | | PyPi | [https://pypi.org/project/python-creole/](https://pypi.org/project/python-creole/) | + ## donation @@ -453,4 +448,4 @@ first source code was written 27.11.2008: [Forum thread (de)](http://www.python- ------------ -``Note: this file is generated from README.creole 2022-08-20 21:51:36 with "python-creole"``
\ No newline at end of file +``Note: this file is generated from README.creole 2022-08-20 22:35:17 with "python-creole"``
\ No newline at end of file @@ -268,11 +268,6 @@ unittests # Run pytest via tox with all environments: ~/python-creole$ make tox - - # Run pytest via tox with one Python version: - ~/python-creole$ make tox-py38 - ~/python-creole$ make tox-py37 - ~/python-creole$ make tox-py36 ------------ make targets @@ -286,16 +281,13 @@ To see all make targets, just call ``make``: help List all commands install-poetry install or update poetry install install python-creole via poetry + update Update the dependencies as according to the pyproject.toml file lint Run code formatters and linter fix-code-style Fix code formatting tox-listenvs List all tox test environments tox Run pytest via tox with all environments - tox-py36 Run pytest via tox with *python v3.6* - tox-py37 Run pytest via tox with *python v3.7* - tox-py38 Run pytest via tox with *python v3.8* - tox-py39 Run pytest via tox with *python v3.9* pytest Run pytest - update-rst-readme update README.rst from README.creole + update-readmes update README.rst from README.creole publish Release new version to PyPi -------------------- @@ -311,7 +303,11 @@ Note: In this case you must install **docutils**! See above. history ======= -* *dev* - `compare v1.4.10...master <https://github.com/jedie/python-creole/compare/v1.4.10...master>`_ +* `*dev* <https://github.com/jedie/python-creole/compare/v1.5.0...master>`_ + + * TBC + +* `v1.5.0 - 2022-08-20 <https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0>`_ * NEW: html2markdown @@ -325,9 +321,7 @@ history * Rename git ``master`` branch to ``main``. - * TBC - -* v1.4.10 - 2021-05-11 - `compare v1.4.9...v1.4.10 <https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10>`_ +* `v1.4.10 - 2021-05-11 <https://github.com/jedie/python-creole/compare/v1.4.9...v1.4.10>`_ * Update some string formatting to f-strings @@ -337,7 +331,7 @@ history * Remove Travis CI (All tests already running via github actions) -* v1.4.9 - 2020-11-4 - `compare v1.4.8...v1.4.9 <https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9>`_ +* `v1.4.9 - 2020-11-4 <https://github.com/jedie/python-creole/compare/v1.4.8...v1.4.9>`_ * Add missing classifier for Python 3.9 (`Contributed by jugmac00 <https://github.com/jedie/python-creole/pull/55>`_) @@ -728,4 +722,4 @@ donation ------------ -``Note: this file is generated from README.creole 2022-08-20 21:51:36 with "python-creole"``
\ No newline at end of file +``Note: this file is generated from README.creole 2022-08-20 22:35:17 with "python-creole"``
\ No newline at end of file diff --git a/creole/emitter/html2markdown_emitter.py b/creole/emitter/html2markdown_emitter.py index 540d53a..83f4ede 100644 --- a/creole/emitter/html2markdown_emitter.py +++ b/creole/emitter/html2markdown_emitter.py @@ -10,12 +10,10 @@ """ -import posixpath - from creole.parser.html_parser import HtmlParser -from creole.parser.html_parser_config import BLOCK_TAGS from creole.shared.base_emitter import BaseEmitter from creole.shared.document_tree import DocNode +from creole.shared.markup_table import MarkupTable class MarkdownEmitter(BaseEmitter): @@ -28,14 +26,30 @@ class MarkdownEmitter(BaseEmitter): self.strict = strict super().__init__(document_tree, *args, **kwargs) - self.table_head_prefix = '= ' - self.table_auto_width = True - def emit(self): """Emit the document represented by self.root DOM tree.""" return self.emit_node(self.root).strip() # FIXME # -------------------------------------------------------------------------- + def table_emit(self, node): + self._table = MarkupTable(head_prefix='', debug_msg=self.debug_msg) + self.emit_children(node) + content = self._table.get_markdown_table() + return f'{content}\n\n' + + def tr_emit(self, node): + self._table.add_tr() + self.emit_children(node) + return '' + + def th_emit(self, node): + self._table.add_th(self.emit_children(node)) + return '' + + def td_emit(self, node): + self._table.add_td(self.emit_children(node)) + return '' + # -------------------------------------------------------------------------- def blockdata_pre_emit(self, node: DocNode): """pre block -> with newline at the end""" diff --git a/creole/shared/markup_table.py b/creole/shared/markup_table.py index 54b4763..70611c2 100644 --- a/creole/shared/markup_table.py +++ b/creole/shared/markup_table.py @@ -99,6 +99,23 @@ class MarkupTable: return "\n".join(lines) + def get_markdown_table(self): + """return the table data in markdown markup.""" + cells, widths = self._get_preformat_info() + + lines = [] + for no, row in enumerate(cells): + if no == 1 and self.has_header: + cells = ['-' * (width - 2) for width in widths] + lines.append(f'| {" | ".join(cells)} |') + cells = [cell.ljust(width) for cell, width in zip(row, widths)] + lines.append(f'|{"|".join(cells)}|') + + result = "\n".join(lines) + + self.debug_msg("Table.get_table_markup", result) + return result + if __name__ == '__main__': import doctest diff --git a/creole/tests/test_html2markdown.py b/creole/tests/test_html2markdown.py index 975a15d..c587928 100644 --- a/creole/tests/test_html2markdown.py +++ b/creole/tests/test_html2markdown.py @@ -97,3 +97,48 @@ class MarkdownTests(BaseCreoleTest): ), # debug=True, ) + + def test_table(self): + self.assert_html2markdown( + markdown_string=cleandoc( + ''' + | A1 | B1 | + | A2 | B2 | + ''' + ), + html_string=cleandoc( + ''' + <table> + <tr><td>A1</td><td>B1</td></tr> + <tr><td>A2</td><td>B2</td></tr> + </table> + ''' + ), + # debug=True, + ) + self.assert_html2markdown( + markdown_string=cleandoc( + ''' + # A Table + + | Foo | Bar | + | --- | --- | + | A1 | B1 | + | A2 | B2 | + + Text after the table. + ''' + ), + html_string=cleandoc( + ''' + <h1>A Table</h1> + <table> + <tr><th>Foo</th><th>Bar</th></tr> + <tr><td>A1</td><td>B1</td></tr> + <tr><td>A2</td><td>B2</td></tr> + </table> + <p>Text after the table.</p> + ''' + ), + # debug=True, + ) @@ -7,7 +7,7 @@ """ -from creole import creole2html, html2creole, html2rest, html2textile +from creole import creole2html, html2creole, html2markdown, html2rest, html2textile source_creole = """\ @@ -15,7 +15,7 @@ source_creole = """\ You can convert from: -* **creole2html**, **html2creole**, **html2rest**, //html2textile// +* **creole2html**, **html2creole**, **html2rest**, **html2markdown**, //html2textile// === a table: @@ -25,7 +25,7 @@ You can convert from: ---- -More info on our [[http://code.google.com/p/python-creole/|Homepage]].""" +More info on our [[https://github.com/jedie/python-creole/|Homepage]].""" if __name__ == "__main__": @@ -44,3 +44,7 @@ if __name__ == "__main__": print("\n\n" + "_" * 79 + "\n*** Convert html into textile: ***\n\n") textile = html2textile(html) print(textile) + + print("\n\n" + "_" * 79 + "\n*** Convert html into markdown: ***\n\n") + markdown = html2markdown(html) + print(markdown) |