Skip to content

Commit dbfbe83

Browse files
committed
Made fold marker strip configurable via --strip-regex.
1 parent 26bc4dd commit dbfbe83

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dist:
5353
examples: PYTHONPATH = .
5454
examples:
5555
PYTHONPATH=.
56-
bin/yaml2rst examples/main.yml examples/main.rst
56+
bin/yaml2rst examples/main.yml examples/main.rst --strip-regex '\s*(:?\[{3}|\]{3})\d?$$'
5757
rst2html --stylesheet=examples/demo.css examples/main.rst | grep --invert-match --fixed-strings '<meta name="generator"' > examples/main.html
5858
rst2html --stylesheet=examples/demo.css tests/patternsTest.rst > tests/patternsTest.html
5959

README.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ allows to process the YAML file directly without any pre-processing.
2121

2222
Usage::
2323

24-
yaml2rst [-h] infile outfile
24+
yaml2rst [-h] [--strip-regex regex] infile outfile
2525

2626
positional arguments:
27-
infile YAML-file to read (`-` for stdin)
28-
outfile rst-file to write (`-` for stdout)
27+
infile YAML-file to read (`-` for stdin)
28+
outfile rst-file to write (`-` for stdout)
2929

3030
optional arguments:
31-
-h, --help show this help message and exit
31+
-h, --help show this help message and exit
32+
--strip-regex regex Regex which will remove everything it matches. Can be
33+
used to remove fold markers from headings for example.
34+
Example to strip out [[[,]]] fold markers:
35+
'\s*(:?\[{3}|\]{3})\d?$'. Check the README for more
36+
details.
3237

3338

3439
How it works
@@ -43,6 +48,10 @@ Additionally at the start and at the end of a "code"-block, lines are
4348
added as required by reStructuredText. Also at the begin of a
4449
"code"-block, a ``::`` is added if required.
4550

51+
``--strip-regex`` can be used to remove matching characters from text-lines
52+
when needed. Refer to documentation about
53+
`Folding marks support <docs/fold-markers.rst>`_ for details.
54+
4655

4756
Examples
4857
-------------

bin/yaml2rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import yaml2rst
3131
import argparse
3232
import sys
3333

34-
def main(infilename, outfilename):
34+
def main(infilename, outfilename, strip_regex):
3535
if infilename == '-':
3636
infh = sys.stdin
3737
else:
@@ -40,7 +40,7 @@ def main(infilename, outfilename):
4040
outfh = sys.stdout
4141
else:
4242
outfh = open(outfilename, "w")
43-
for l in yaml2rst.convert(infh.readlines()):
43+
for l in yaml2rst.convert(infh.readlines(), strip_regex):
4444
print(l.rstrip(), file=outfh)
4545

4646

@@ -49,6 +49,10 @@ parser.add_argument('infilename', metavar='infile',
4949
help="YAML-file to read (`-` for stdin)")
5050
parser.add_argument('outfilename', metavar='outfile',
5151
help="rst-file to write (`-` for stdout)")
52+
parser.add_argument('--strip-regex', metavar='regex',
53+
help="Regex which will remove everything it matches."
54+
" Can be used to remove fold markers from headings for example."
55+
" Example to strip out [[[,]]] fold markers: '\s*(:?\[{3}|\]{3})\d?$'."
56+
" Check the README for more details.")
5257
args = parser.parse_args()
5358
main(**vars(args))
54-

tests/test_convert.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def _write_pattern(self, *lines):
5353
print(line, file=self._outfile)
5454
print(file=self._outfile)
5555

56-
def _test(self, text, expected):
56+
def _test(self, text, expected, strip_regex=None):
5757
text = textwrap.dedent(text)
5858
if isinstance(expected, basestring):
5959
expected = textwrap.dedent(expected).splitlines()
6060
self._write_pattern(*expected)
61-
res = list(yaml2rst.convert(text.splitlines()))
61+
res = list(yaml2rst.convert(text.splitlines(), strip_regex))
6262
self.assertListEqual(expected, res)
63-
63+
6464
def test_no_text_at_all(self):
6565
text = """\
6666
---
@@ -220,3 +220,22 @@ def test_no_more_indent(self):
220220
Some code at outer level
221221
"""
222222
self._test(text, expected)
223+
224+
def test_strip_regex(self):
225+
text = """\
226+
---
227+
# Heading [[[1
228+
# =======
229+
key: value
230+
# ]]]
231+
"""
232+
expected = """\
233+
Heading
234+
=======
235+
::
236+
237+
key: value
238+
239+
240+
"""
241+
self._test(text, expected, '\s*(:?\[{3}|\]{3})\d?$')

yaml2rst.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ def get_indent(line):
8989
return indent
9090

9191

92-
def convert(lines):
92+
def get_stripped_line(line, strip_regex):
93+
if strip_regex:
94+
line = re.sub(strip_regex, "", line)
95+
return line
96+
97+
98+
def convert(lines, strip_regex):
9399
state = STATE_TEXT
94100
last_text_line = ''
95101
last_indent = ''
@@ -101,9 +107,7 @@ def convert(lines):
101107
elif line.startswith('# ') or line == '#':
102108
if state != STATE_TEXT:
103109
yield ''
104-
# Filter out [[[\d and ]]] at the end of a documentation line.
105-
# Those sequences can be used for folding sections.
106-
line = re.sub(r"\s*(:?\[{3}|\]{3})\d?$", "", line)
110+
line = get_stripped_line(line, strip_regex)
107111
line = last_text_line = line[2:]
108112
yield line
109113
last_indent = get_indent(line)* ' '

0 commit comments

Comments
 (0)