diff --git a/Makefile b/Makefile
index 5ccd027..f298351 100644
--- a/Makefile
+++ b/Makefile
@@ -53,7 +53,7 @@ dist:
examples: PYTHONPATH = .
examples:
PYTHONPATH=.
- bin/yaml2rst examples/main.yml examples/main.rst
+ bin/yaml2rst examples/main.yml examples/main.rst --strip-regex '\s*(:?\[{3}|\]{3})\d?$$'
rst2html --stylesheet=examples/demo.css examples/main.rst | grep --invert-match --fixed-strings ' examples/main.html
rst2html --stylesheet=examples/demo.css tests/patternsTest.rst > tests/patternsTest.html
diff --git a/README.rst b/README.rst
index e344f3a..3974832 100644
--- a/README.rst
+++ b/README.rst
@@ -21,14 +21,19 @@ allows to process the YAML file directly without any pre-processing.
Usage::
- yaml2rst [-h] infile outfile
+ yaml2rst [-h] [--strip-regex regex] infile outfile
positional arguments:
- infile YAML-file to read (`-` for stdin)
- outfile rst-file to write (`-` for stdout)
+ infile YAML-file to read (`-` for stdin)
+ outfile rst-file to write (`-` for stdout)
optional arguments:
- -h, --help show this help message and exit
+ -h, --help show this help message and exit
+ --strip-regex regex Regex which will remove everything it matches. Can be
+ used to remove fold markers from headings for example.
+ Example to strip out [[[,]]] fold markers:
+ '\s*(:?\[{3}|\]{3})\d?$'. Check the README for more
+ details.
How it works
@@ -43,6 +48,10 @@ Additionally at the start and at the end of a "code"-block, lines are
added as required by reStructuredText. Also at the begin of a
"code"-block, a ``::`` is added if required.
+``--strip-regex`` can be used to remove matching characters from text-lines
+when needed. Refer to documentation about
+`Folding marks support `_ for details.
+
Examples
-------------
diff --git a/bin/yaml2rst b/bin/yaml2rst
index ff669c9..16344fb 100755
--- a/bin/yaml2rst
+++ b/bin/yaml2rst
@@ -32,7 +32,7 @@ import argparse
import sys
-def main(infilename, outfilename):
+def main(infilename, outfilename, strip_regex):
if infilename == '-':
infh = sys.stdin
else:
@@ -41,7 +41,7 @@ def main(infilename, outfilename):
outfh = sys.stdout
else:
outfh = open(outfilename, "w")
- for l in yaml2rst.convert(infh.readlines()):
+ for l in yaml2rst.convert(infh.readlines(), strip_regex):
print(l.rstrip(), file=outfh)
@@ -50,5 +50,10 @@ parser.add_argument('infilename', metavar='infile',
help="YAML-file to read (`-` for stdin)")
parser.add_argument('outfilename', metavar='outfile',
help="rst-file to write (`-` for stdout)")
+parser.add_argument('--strip-regex', metavar='regex',
+ help="Regex which will remove everything it matches."
+ " Can be used to remove fold markers from headings for example."
+ " Example to strip out [[[,]]] fold markers: '\s*(:?\[{3}|\]{3})\d?$'."
+ " Check the README for more details.")
args = parser.parse_args()
main(**vars(args))
diff --git a/tests/test_convert.py b/tests/test_convert.py
index 799ce01..3795b32 100644
--- a/tests/test_convert.py
+++ b/tests/test_convert.py
@@ -53,14 +53,14 @@ def _write_pattern(self, *lines):
print(line, file=self._outfile)
print(file=self._outfile)
- def _test(self, text, expected):
+ def _test(self, text, expected, strip_regex=None):
text = textwrap.dedent(text)
if isinstance(expected, basestring):
expected = textwrap.dedent(expected).splitlines()
self._write_pattern(*expected)
- res = list(yaml2rst.convert(text.splitlines()))
+ res = list(yaml2rst.convert(text.splitlines(), strip_regex))
self.assertListEqual(expected, res)
-
+
def test_no_text_at_all(self):
text = """\
---
@@ -265,3 +265,22 @@ def test_nested_enumeration(self):
Some code under list-entry 2
"""
self._test(text, expected)
+
+ def test_strip_regex(self):
+ text = """\
+ ---
+ # Heading [[[1
+ # =======
+ key: value
+ # ]]]
+ """
+ expected = """\
+ Heading
+ =======
+ ::
+
+ key: value
+
+
+ """
+ self._test(text, expected, '\s*(:?\[{3}|\]{3})\d?$')
diff --git a/yaml2rst.py b/yaml2rst.py
index 9e04b0e..1d6fc05 100644
--- a/yaml2rst.py
+++ b/yaml2rst.py
@@ -92,7 +92,13 @@ def get_indent(line):
return indent
-def convert(lines):
+def get_stripped_line(line, strip_regex):
+ if strip_regex:
+ line = re.sub(strip_regex, "", line)
+ return line
+
+
+def convert(lines, strip_regex):
state = STATE_TEXT
last_text_line = ''
last_indent = ''
@@ -104,9 +110,7 @@ def convert(lines):
elif line.startswith('# ') or line == '#':
if state != STATE_TEXT:
yield ''
- # Filter out [[[\d and ]]] at the end of a documentation line.
- # Those sequences can be used for folding sections.
- line = re.sub(r"\s*(:?\[{3}|\]{3})\d?$", "", line)
+ line = get_stripped_line(line, strip_regex)
line = last_text_line = line[2:]
yield line
last_indent = get_indent(line) * ' '