Skip to content

Commit

Permalink
Merge pull request #212 from michaelnt/no-levels
Browse files Browse the repository at this point in the history
Add option to omit levels from all items or only non heading items
  • Loading branch information
jacebrowning committed Jan 18, 2017
2 parents 40f9fad + e685553 commit 90b9337
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
5 changes: 4 additions & 1 deletion doorstop/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,11 @@ def _publish(subs, shared):
help="limit line width on text output")
sub.add_argument('-C', '--no-child-links', action='store_true',
help="do not include child links on items")
sub.add_argument('-L', '--no-body-levels', action='store_true',
sub.add_argument('-L', '--no-body-levels', dest='no_body_levels', action='store_true',
default=False,
help="do not include levels on non-heading items")
sub.add_argument('--no-levels', choices=['all', 'body'],
help="do not include levels on heading and non-heading or non-heading items")


if __name__ == '__main__': # pragma: no cover (manual test)
Expand Down
11 changes: 11 additions & 0 deletions doorstop/cli/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,17 @@ def test_publish_document_without_body_levels(self):
self.assertIs(None, main(['publish', 'tut', '--no-body-levels']))
self.assertFalse(settings.PUBLISH_BODY_LEVELS)

def test_publish_document_no_body_levels(self):
"""Verify 'doorstop publish' can create output without body levels."""
self.assertIs(None, main(['publish', 'tut', '--no-levels=body']))
self.assertFalse(settings.PUBLISH_BODY_LEVELS)

def test_publish_document_no_body_or_heading_levels(self):
"""Verify 'doorstop publish' can create output without heading or body levels."""
self.assertIs(None, main(['publish', 'tut', '--no-levels=all']))
self.assertFalse(settings.PUBLISH_BODY_LEVELS)
self.assertFalse(settings.PUBLISH_HEADING_LEVELS)

def test_publish_document_error_empty(self):
"""Verify 'doorstop publish' returns an error in an empty folder."""
os.chdir(self.temp)
Expand Down
10 changes: 8 additions & 2 deletions doorstop/cli/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ast
from argparse import ArgumentTypeError
import logging
import warnings

from doorstop import common
from doorstop import settings
Expand Down Expand Up @@ -112,8 +113,13 @@ def configure_settings(args):
# Parse `publish` settings
if hasattr(args, 'no_child_links') and args.no_child_links is not None:
settings.PUBLISH_CHILD_LINKS = args.no_child_links is False
if hasattr(args, 'no_body_levels') and args.no_body_levels is not None:
settings.PUBLISH_BODY_LEVELS = args.no_body_levels is False
if hasattr(args, 'no_body_levels'):
settings.PUBLISH_BODY_LEVELS = not args.no_body_levels
msg = "'--no-body-levels' option will be removed in a future release"
warnings.warn(msg, DeprecationWarning)
if hasattr(args, 'no_levels') and args.no_levels is not None:
settings.PUBLISH_BODY_LEVELS = False
settings.PUBLISH_HEADING_LEVELS = args.no_levels != 'all'


def literal_eval(literal, error=None, default=None):
Expand Down
10 changes: 8 additions & 2 deletions doorstop/core/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def _lines_text(obj, indent=8, width=79, **_):
if item.heading:

# Level and Text
yield "{l:<{s}}{t}".format(l=level, s=indent, t=item.text)
if settings.PUBLISH_HEADING_LEVELS:
yield "{l:<{s}}{t}".format(l=level, s=indent, t=item.text)
else:
yield "{t}".format(t=item.text)

else:

Expand Down Expand Up @@ -277,7 +280,10 @@ def _lines_markdown(obj, linkify=False):
if item.heading:

# Level and Text
standard = "{h} {l} {t}".format(h=heading, l=level, t=item.text)
if settings.PUBLISH_HEADING_LEVELS:
standard = "{h} {l} {t}".format(h=heading, l=level, t=item.text)
else:
standard = "{h} {t}".format(h=heading, t=item.text)
attr_list = _format_md_attr_list(item, linkify)
yield standard + attr_list

Expand Down
30 changes: 30 additions & 0 deletions doorstop/core/test/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ def test_lines_text_item_heading(self):
# Assert
self.assertEqual(expected, text)

@patch('doorstop.settings.PUBLISH_HEADING_LEVELS', False)
def test_lines_text_item_heading_no_heading_levels(self):
"""Verify an item heading level can be ommitted."""
expected = "Heading\n\n"
lines = publisher.publish_lines(self.item, '.txt')
# Act
text = ''.join(line + '\n' for line in lines)
# Assert
self.assertEqual(expected, text)

@patch('doorstop.settings.PUBLISH_CHILD_LINKS', False)
def test_lines_text_item_normative(self):
"""Verify text can be published from an item (normative)."""
Expand Down Expand Up @@ -198,6 +208,16 @@ def test_lines_markdown_item_heading(self):
# Assert
self.assertEqual(expected, text)

@patch('doorstop.settings.PUBLISH_HEADING_LEVELS', False)
def test_lines_markdown_item_heading_no_heading_levels(self):
"""Verify an item heading level can be ommitted."""
expected = "## Heading {: #req3 }\n\n"
# Act
lines = publisher.publish_lines(self.item, '.md', linkify=True)
text = ''.join(line + '\n' for line in lines)
# Assert
self.assertEqual(expected, text)

@patch('doorstop.settings.PUBLISH_CHILD_LINKS', False)
def test_lines_markdown_item_normative(self):
"""Verify Markdown can be published from an item (normative)."""
Expand Down Expand Up @@ -259,6 +279,16 @@ def test_lines_html_item(self):
# Assert
self.assertEqual(expected, text)

@patch('doorstop.settings.PUBLISH_HEADING_LEVELS', False)
def test_lines_html_item_no_heading_levels(self):
"""Verify an item heading level can be ommitted."""
expected = '<h2>Heading</h2>\n'
# Act
lines = publisher.publish_lines(self.item, '.html')
text = ''.join(line + '\n' for line in lines)
# Assert
self.assertEqual(expected, text)

def test_lines_html_item_linkify(self):
"""Verify HTML (hyper) can be published from an item."""
expected = '<h2 id="req3">1.1 Heading</h2>\n'
Expand Down
1 change: 1 addition & 0 deletions doorstop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
# Publishing settings
PUBLISH_CHILD_LINKS = True # include child links when publishing
PUBLISH_BODY_LEVELS = True # include levels on non-header items
PUBLISH_HEADING_LEVELS = True # include levels on header items

# Version control settings
ADDREMOVE_FILES = True # automatically add/remove new/changed files
Expand Down

0 comments on commit 90b9337

Please sign in to comment.