Skip to content

Commit

Permalink
Implemented --yaml-strip-regex to strip out things in YAML state.
Browse files Browse the repository at this point in the history
Related to: debops#3
Related to: debops/docs#156
  • Loading branch information
ypid committed Jul 12, 2016
1 parent 153b830 commit 0518586
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ dist:
examples: PYTHONPATH = .
examples:
PYTHONPATH=.
sed --regexp-extended 's/(\.\. )envvar(::)/\1note\2/;' examples/fold-markers-debops.yml examples/main.yml | bin/yaml2rst - examples/main.rst --strip-regex '\s*(:?\[{3}|\]{3})\d?$$'
sed --regexp-extended 's/(\.\. )envvar(::)/\1note\2/;' examples/fold-markers-debops.yml examples/main.yml \
| bin/yaml2rst - examples/main.rst --strip-regex '\s*(:?\[{3}|\]{3})\d?$$' --yaml-strip-regex '^\s{65,66}#\s\]{3}\d?$$'
## --no-generator does not do the trick on Debian Jessie.
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
7 changes: 5 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, strip_regex):
def main(infilename, outfilename, strip_regex, yaml_strip_regex):
if infilename == '-':
infh = sys.stdin
else:
Expand All @@ -41,7 +41,7 @@ def main(infilename, outfilename, strip_regex):
outfh = sys.stdout
else:
outfh = open(outfilename, "w")
for l in yaml2rst.convert(infh.readlines(), strip_regex):
for l in yaml2rst.convert(infh.readlines(), strip_regex, yaml_strip_regex):
print(l.rstrip(), file=outfh)


Expand All @@ -55,5 +55,8 @@ parser.add_argument('--strip-regex', metavar='regex',
" 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.")
parser.add_argument('--yaml-strip-regex', metavar='regex',
help="Same usage as --strip-regex except that this regex substitution"
"is preformed on the YAMl part of the file as opposed RST part.")
args = parser.parse_args()
main(**vars(args))
7 changes: 0 additions & 7 deletions examples/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ <h2><a class="toc-backref" href="#id1">Main configuration</a></h2>
<p>Some text describing this boolean.</p>
<pre class="literal-block">
example__enabled: True

# ]]]
</pre>
<div class="note">
<p class="first admonition-title">Note</p>
Expand All @@ -84,8 +82,6 @@ <h2><a class="toc-backref" href="#id1">Main configuration</a></h2>
<p>List of additional APT packages which will be installed by the role.</p>
<pre class="literal-block">
example__packages: []
# ]]]
# ]]]
</pre>
</div>
<div class="section" id="something">
Expand All @@ -97,9 +93,6 @@ <h2><a class="toc-backref" href="#id2">Something</a></h2>
<p>Some text describing this list.</p>
<pre class="literal-block">
example__periodic: []
# ]]]
# ]]]
# ]]]
</pre>
</div>
</div>
Expand Down
12 changes: 6 additions & 6 deletions examples/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ Some text describing this boolean.

example__enabled: True

# ]]]


.. note:: example__packages

List of additional APT packages which will be installed by the role.
::

example__packages: []
# ]]]
# ]]]



Something
-------------
Expand All @@ -38,9 +38,9 @@ Some text describing this list.
::

example__periodic: []
# ]]]
# ]]]
# ]]]




collectd
================
Expand Down
11 changes: 6 additions & 5 deletions yaml2rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_stripped_line(line, strip_regex):
return line


def convert(lines, strip_regex):
def convert(lines, strip_regex, yaml_strip_regex):
state = STATE_TEXT
last_text_line = ''
last_indent = ''
Expand All @@ -124,16 +124,17 @@ def convert(lines, strip_regex):
if not last_text_line.endswith('::'):
yield last_indent + '::'
yield ''
line = get_stripped_line(line, yaml_strip_regex)
yield last_indent + ' ' + line
state = STATE_YAML


def convert_text(yaml_text, strip_regex=None):
return '\n'.join(convert(yaml_text.splitlines(), strip_regex))
def convert_text(yaml_text, strip_regex=None, yaml_strip_regex=None):
return '\n'.join(convert(yaml_text.splitlines(), strip_regex, yaml_strip_regex))


def convert_file(infilename, outfilename, strip_regex=None):
def convert_file(infilename, outfilename, strip_regex=None, yaml_strip_regex=None):
with open(infilename) as infh:
with open(outfilename, "w") as outfh:
for l in convert(infh.readlines(), strip_regex):
for l in convert(infh.readlines(), strip_regex, yaml_strip_regex):
print(l.rstrip(), file=outfh)

0 comments on commit 0518586

Please sign in to comment.