Skip to content

Commit

Permalink
Made fold marker strip configurable via --strip-regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
ypid committed Jun 6, 2016
1 parent 3702f9b commit 63e020d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<meta name="generator"' > examples/main.html
rst2html --stylesheet=examples/demo.css tests/patternsTest.rst > tests/patternsTest.html

Expand Down
17 changes: 13 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <docs/fold-markers.rst>`_ for details.


Examples
-------------
Expand Down
9 changes: 7 additions & 2 deletions bin/yaml2rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import argparse
import sys


def main(infilename, outfilename):
def main(infilename, outfilename, strip_regex):
if infilename == '-':
infh = sys.stdin
else:
Expand All @@ -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)


Expand All @@ -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))
25 changes: 22 additions & 3 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """\
---
Expand Down Expand Up @@ -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?$')
12 changes: 8 additions & 4 deletions yaml2rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -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) * ' '
Expand Down

0 comments on commit 63e020d

Please sign in to comment.