From e48e6e8b59cafa1dba5046d7a0e3116bb189c23a Mon Sep 17 00:00:00 2001 From: kyleknap Date: Wed, 4 Mar 2015 16:18:09 -0800 Subject: [PATCH] Added various style methods Includes a hidden doc tree and sphinx reference labels --- bcdoc/style.py | 17 ++++++++++++++ tests/unit/test_style.py | 48 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/bcdoc/style.py b/bcdoc/style.py index 87708b9..57ef49d 100644 --- a/bcdoc/style.py +++ b/bcdoc/style.py @@ -196,6 +196,14 @@ def start_a(self, attrs=None): def link_target_definition(self, refname, link): self.doc.writeln('.. _%s: %s' % (refname, link)) + def sphinx_reference_label(self, label, text=None): + if text is None: + text = label + if self.doc.target == 'html': + self.doc.write(':ref:`%s <%s>`' % (text, label)) + else: + self.doc.write(text) + def end_a(self): self.doc.do_translation = False if self.a_href: @@ -306,3 +314,12 @@ def tocitem(self, item, file_name=None): else: self.doc.writeln(' %s' % item) + def hidden_toctree(self): + if self.doc.target == 'html': + self.doc.write('\n.. toctree::\n') + self.doc.write(' :maxdepth: 1\n') + self.doc.write(' :hidden:\n\n') + + def hidden_tocitem(self, item): + if self.doc.target == 'html': + self.tocitem(item) diff --git a/tests/unit/test_style.py b/tests/unit/test_style.py index e1be1a3..1b9993a 100644 --- a/tests/unit/test_style.py +++ b/tests/unit/test_style.py @@ -120,6 +120,27 @@ def test_toctree_man(self): self.assertEqual(style.doc.getvalue(), six.b('\n\n\n* foo\n\n\n* bar\n\n')) + def test_hidden_toctree_html(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'html' + style.hidden_toctree() + style.hidden_tocitem('foo') + style.hidden_tocitem('bar') + self.assertEqual( + style.doc.getvalue(), + six.b('\n.. toctree::\n :maxdepth: 1' + '\n :hidden:\n\n foo\n bar\n')) + + def test_hidden_toctree_non_html(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'man' + style.hidden_toctree() + style.hidden_tocitem('foo') + style.hidden_tocitem('bar') + self.assertEqual( + style.doc.getvalue(), + six.b('')) + def test_escape_href_link(self): style = ReSTStyle(ReSTDocument()) style.start_a(attrs=[('href', 'http://example.org')]) @@ -134,4 +155,29 @@ def test_handle_no_text_hrefs(self): style = ReSTStyle(ReSTDocument()) style.start_a(attrs=[('href', 'http://example.org')]) style.end_a() - self.assertEqual(style.doc.getvalue(), six.b('``_ ')) + self.assertEqual(style.doc.getvalue(), + six.b('``_ ')) + + def test_sphinx_reference_label_html(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'html' + style.sphinx_reference_label('foo', 'bar') + self.assertEqual(style.doc.getvalue(), six.b(':ref:`bar `')) + + def test_sphinx_reference_label_html_no_text(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'html' + style.sphinx_reference_label('foo') + self.assertEqual(style.doc.getvalue(), six.b(':ref:`foo `')) + + def test_sphinx_reference_label_non_html(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'man' + style.sphinx_reference_label('foo', 'bar') + self.assertEqual(style.doc.getvalue(), six.b('bar')) + + def test_sphinx_reference_label_non_html_no_text(self): + style = ReSTStyle(ReSTDocument()) + style.doc.target = 'man' + style.sphinx_reference_label('foo') + self.assertEqual(style.doc.getvalue(), six.b('foo'))