From 3e1667290b9e52c52d249abcc5f00eacfb42def4 Mon Sep 17 00:00:00 2001 From: tk0miya Date: Mon, 11 Aug 2014 22:36:31 +0900 Subject: [PATCH 1/3] Rename :filename: option of code-block directive to :caption: --- sphinx/directives/code.py | 20 ++++----- sphinx/themes/agogo/static/agogo.css_t | 2 +- sphinx/themes/basic/static/basic.css_t | 8 ++-- sphinx/themes/default/static/default.css_t | 2 +- sphinx/themes/nature/static/nature.css_t | 2 +- sphinx/themes/pyramid/static/pyramid.css_t | 2 +- .../themes/sphinxdoc/static/sphinxdoc.css_t | 2 +- .../traditional/static/traditional.css_t | 2 +- sphinx/writers/html.py | 6 +-- sphinx/writers/latex.py | 6 +-- tests/roots/test-directive-code/caption.rst | 21 ++++++++++ tests/roots/test-directive-code/index.rst | 1 + tests/test_directive_code.py | 42 +++++++++++++++++++ 13 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 tests/roots/test-directive-code/caption.rst diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 7f08b8142..c0b27dff2 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -74,7 +74,7 @@ class CodeBlock(Directive): 'dedent': int, 'lineno-start': int, 'emphasize-lines': directives.unchanged_required, - 'filename': directives.unchanged_required, + 'caption': directives.unchanged_required, } def run(self): @@ -98,9 +98,9 @@ class CodeBlock(Directive): literal = nodes.literal_block(code, code) literal['language'] = self.arguments[0] - filename = self.options.get('filename') - if filename: - literal['filename'] = filename + caption = self.options.get('caption') + if caption: + literal['caption'] = caption literal['linenos'] = 'linenos' in self.options or \ 'lineno-start' in self.options extra_args = literal['highlight_args'] = {} @@ -137,7 +137,7 @@ class LiteralInclude(Directive): 'prepend': directives.unchanged_required, 'append': directives.unchanged_required, 'emphasize-lines': directives.unchanged_required, - 'filename': directives.unchanged, + 'caption': directives.unchanged, } def run(self): @@ -240,11 +240,11 @@ class LiteralInclude(Directive): retnode['language'] = self.options['language'] retnode['linenos'] = 'linenos' in self.options or \ 'lineno-start' in self.options - filename = self.options.get('filename') - if filename is not None: - if not filename: - filename = self.arguments[0] - retnode['filename'] = filename + caption = self.options.get('caption') + if caption is not None: + if not caption: + caption = self.arguments[0] + retnode['caption'] = caption extra_args = retnode['highlight_args'] = {} if hl_lines is not None: extra_args['hl_lines'] = hl_lines diff --git a/sphinx/themes/agogo/static/agogo.css_t b/sphinx/themes/agogo/static/agogo.css_t index 9cd504142..db4a621a4 100644 --- a/sphinx/themes/agogo/static/agogo.css_t +++ b/sphinx/themes/agogo/static/agogo.css_t @@ -467,7 +467,7 @@ div.viewcode-block:target { border-bottom: 1px solid #ac9; } -div.code-block-filename { +div.code-block-caption { background-color: #ddd; color: #333; padding: 2px 5px; diff --git a/sphinx/themes/basic/static/basic.css_t b/sphinx/themes/basic/static/basic.css_t index d4561bfa1..6fccb86eb 100644 --- a/sphinx/themes/basic/static/basic.css_t +++ b/sphinx/themes/basic/static/basic.css_t @@ -475,17 +475,17 @@ table.highlighttable td { padding: 0 0.5em 0 0.5em; } -div.code-block-filename { +div.code-block-caption { padding: 2px 5px; font-size: small; } -div.code-block-filename tt { +div.code-block-caption tt { background-color: transparent; } -div.code-block-filename + pre, -div.code-block-filename + div.highlight > pre { +div.code-block-caption + pre, +div.code-block-caption + div.highlight > pre { margin-top: 0; } diff --git a/sphinx/themes/default/static/default.css_t b/sphinx/themes/default/static/default.css_t index ae4012d0f..1493c5d98 100644 --- a/sphinx/themes/default/static/default.css_t +++ b/sphinx/themes/default/static/default.css_t @@ -309,7 +309,7 @@ div.viewcode-block:target { border-bottom: 1px solid #ac9; } -div.code-block-filename { +div.code-block-caption { color: #efefef; background-color: #1c4e63; } diff --git a/sphinx/themes/nature/static/nature.css_t b/sphinx/themes/nature/static/nature.css_t index 7909e8132..09573eada 100644 --- a/sphinx/themes/nature/static/nature.css_t +++ b/sphinx/themes/nature/static/nature.css_t @@ -244,7 +244,7 @@ div.viewcode-block:target { border-bottom: 1px solid #ac9; } -div.code-block-filename { +div.code-block-caption { background-color: #ddd; color: #222; border: 1px solid #C6C9CB; diff --git a/sphinx/themes/pyramid/static/pyramid.css_t b/sphinx/themes/pyramid/static/pyramid.css_t index 053f61fa0..dc015bb9f 100644 --- a/sphinx/themes/pyramid/static/pyramid.css_t +++ b/sphinx/themes/pyramid/static/pyramid.css_t @@ -341,7 +341,7 @@ tt.xref { font-style: normal; } -div.code-block-filename { +div.code-block-caption { background-color: #ddd; color: #222; } diff --git a/sphinx/themes/sphinxdoc/static/sphinxdoc.css_t b/sphinx/themes/sphinxdoc/static/sphinxdoc.css_t index 9f90d56fc..cd781d29b 100644 --- a/sphinx/themes/sphinxdoc/static/sphinxdoc.css_t +++ b/sphinx/themes/sphinxdoc/static/sphinxdoc.css_t @@ -338,7 +338,7 @@ div.viewcode-block:target { border-bottom: 1px solid #ac9; } -div.code-block-filename { +div.code-block-caption { background-color: #ddd; color: #222; border: 1px solid #ccc; diff --git a/sphinx/themes/traditional/static/traditional.css_t b/sphinx/themes/traditional/static/traditional.css_t index 4b6e5c413..9a5d56dc5 100644 --- a/sphinx/themes/traditional/static/traditional.css_t +++ b/sphinx/themes/traditional/static/traditional.css_t @@ -703,6 +703,6 @@ div.viewcode-block:target { padding: 0 10px; } -div.code-block-filename { +div.code-block-caption { background-color: #cceeff; } diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 2d3366924..b856da069 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -275,9 +275,9 @@ class HTMLTranslator(BaseTranslator): **highlight_args) starttag = self.starttag(node, 'div', suffix='', CLASS='highlight-%s' % lang) - if 'filename' in node: - starttag += '
%s
' % ( - node['filename'],) + if 'caption' in node: + starttag += '
%s
' % ( + node['caption'],) self.body.append(starttag + highlighted + '\n') raise nodes.SkipNode diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index d77c2c541..a7459c924 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1345,11 +1345,11 @@ class LaTeXTranslator(nodes.NodeVisitor): highlight_args['force'] = True if 'linenos' in node: linenos = node['linenos'] - filename = node.get('filename') - if filename: + caption = node.get('caption') + if caption: self.body.append('\n{\\colorbox[rgb]{0.9,0.9,0.9}' '{\\makebox[\\textwidth][l]' - '{\\small\\texttt{%s}}}}\n' % (filename,)) + '{\\small\\texttt{%s}}}}\n' % (caption,)) def warner(msg): self.builder.warn(msg, (self.curfilestack[-1], node.line)) hlcode = self.highlighter.highlight_block(code, lang, warn=warner, diff --git a/tests/roots/test-directive-code/caption.rst b/tests/roots/test-directive-code/caption.rst new file mode 100644 index 000000000..274d0f19d --- /dev/null +++ b/tests/roots/test-directive-code/caption.rst @@ -0,0 +1,21 @@ +Dedent +====== + +Code blocks +----------- + +.. code-block:: ruby + :caption: caption-test.rb + + def ruby? + false + end + + +Literal Include +--------------- + +.. literalinclude:: literal.inc + :language: python + :caption: caption-test.py + :lines: 10-11 diff --git a/tests/roots/test-directive-code/index.rst b/tests/roots/test-directive-code/index.rst index ac7e519e5..dab6b7083 100644 --- a/tests/roots/test-directive-code/index.rst +++ b/tests/roots/test-directive-code/index.rst @@ -2,6 +2,7 @@ test-directive-code =================== .. toctree:: + :glob: * diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index bc2609df8..4c02466dc 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -77,6 +77,27 @@ def test_code_block_dedent(app): assert actual == '\n\n' +@with_app(buildername='html', + srcdir=(test_roots / 'test-directive-code'), + _copy_to_temp=True) +def test_code_block_caption_html(app): + app.builder.build('index') + html = (app.outdir / 'caption.html').text() + caption = '
caption-test.rb
' + assert caption in html + + +@with_app(buildername='latex', + srcdir=(test_roots / 'test-directive-code'), + _copy_to_temp=True) +def test_code_block_caption_latex(app): + app.builder.build('index') + latex = (app.outdir / 'Python.tex').text() + caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' + '{\\small\\texttt{caption-test.rb}}}}') + assert caption in latex + + @with_app(buildername='xml', srcdir=(test_roots / 'test-directive-code'), _copy_to_temp=True) @@ -129,3 +150,24 @@ def test_literal_include_dedent(app): actual = get_dedent_actual(1000) assert actual == '\n\n' + + +@with_app(buildername='html', + srcdir=(test_roots / 'test-directive-code'), + _copy_to_temp=True) +def test_literalinclude_caption_html(app): + app.builder.build('index') + html = (app.outdir / 'caption.html').text() + caption = '
caption-test.py
' + assert caption in html + + +@with_app(buildername='latex', + srcdir=(test_roots / 'test-directive-code'), + _copy_to_temp=True) +def test_literalinclude_caption_latex(app): + app.builder.build('index') + latex = (app.outdir / 'Python.tex').text() + caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' + '{\\small\\texttt{caption-test.py}}}}') + assert caption in latex From 3c1067c0380e07829190a99440cec7bd18c7fa0a Mon Sep 17 00:00:00 2001 From: tk0miya Date: Wed, 20 Aug 2014 12:19:12 +0900 Subject: [PATCH 2/3] Fix #1381 :rfc: and :pep: roles support custom link text --- sphinx/roles.py | 34 ++++++++++++++++++++-------------- tests/root/markup.txt | 2 ++ tests/test_build_html.py | 4 ++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/sphinx/roles.py b/sphinx/roles.py index 122c52853..aaf6272bd 100644 --- a/sphinx/roles.py +++ b/sphinx/roles.py @@ -158,7 +158,7 @@ class XRefRole(object): return [node], [] -def indexmarkup_role(typ, rawtext, etext, lineno, inliner, +def indexmarkup_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): """Role for PEP/RFC references that generate an index entry.""" env = inliner.document.settings.env @@ -166,47 +166,53 @@ def indexmarkup_role(typ, rawtext, etext, lineno, inliner, typ = env.config.default_role else: typ = typ.lower() - text = utils.unescape(etext) + has_explicit_title, title, target = split_explicit_title(text) + title = utils.unescape(title) + target = utils.unescape(target) targetid = 'index-%s' % env.new_serialno('index') indexnode = addnodes.index() targetnode = nodes.target('', '', ids=[targetid]) inliner.document.note_explicit_target(targetnode) if typ == 'pep': indexnode['entries'] = [ - ('single', _('Python Enhancement Proposals; PEP %s') % text, + ('single', _('Python Enhancement Proposals; PEP %s') % target, targetid, '')] anchor = '' - anchorindex = text.find('#') + anchorindex = target.find('#') if anchorindex > 0: - text, anchor = text[:anchorindex], text[anchorindex:] + target, anchor = target[:anchorindex], target[anchorindex:] + if not has_explicit_title: + title = "PEP " + utils.unescape(title) try: - pepnum = int(text) + pepnum = int(target) except ValueError: - msg = inliner.reporter.error('invalid PEP number %s' % text, + msg = inliner.reporter.error('invalid PEP number %s' % target, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] ref = inliner.document.settings.pep_base_url + 'pep-%04d' % pepnum - sn = nodes.strong('PEP '+text, 'PEP '+text) + sn = nodes.strong(title, title) rn = nodes.reference('', '', internal=False, refuri=ref+anchor, classes=[typ]) rn += sn return [indexnode, targetnode, rn], [] elif typ == 'rfc': - indexnode['entries'] = [('single', 'RFC; RFC %s' % text, targetid, '')] + indexnode['entries'] = [('single', 'RFC; RFC %s' % target, targetid, '')] anchor = '' - anchorindex = text.find('#') + anchorindex = target.find('#') if anchorindex > 0: - text, anchor = text[:anchorindex], text[anchorindex:] + target, anchor = target[:anchorindex], target[anchorindex:] + if not has_explicit_title: + title = "RFC " + utils.unescape(title) try: - rfcnum = int(text) + rfcnum = int(target) except ValueError: - msg = inliner.reporter.error('invalid RFC number %s' % text, + msg = inliner.reporter.error('invalid RFC number %s' % target, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum - sn = nodes.strong('RFC '+text, 'RFC '+text) + sn = nodes.strong(title, title) rn = nodes.reference('', '', internal=False, refuri=ref+anchor, classes=[typ]) rn += sn diff --git a/tests/root/markup.txt b/tests/root/markup.txt index 7ce721bab..f6f955e24 100644 --- a/tests/root/markup.txt +++ b/tests/root/markup.txt @@ -132,7 +132,9 @@ Adding \n to test unescaping. *Linking inline markup* * :pep:`8` +* :pep:`Python Enhancement Proposal #8 <8>` * :rfc:`1` +* :rfc:`Request for Comments #1 <1>` * :envvar:`HOME` * :keyword:`with` * :token:`try statement ` diff --git a/tests/test_build_html.py b/tests/test_build_html.py index d13c7ac6f..17a09eae9 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -129,8 +129,12 @@ HTML_XPATH = { (".//li/code/em/span[@class='pre']", '^i$'), (".//a[@href='http://www.python.org/dev/peps/pep-0008']" "[@class='pep reference external']/strong", 'PEP 8'), + (".//a[@href='http://www.python.org/dev/peps/pep-0008']" + "[@class='pep reference external']/strong", 'Python Enhancement Proposal #8'), (".//a[@href='http://tools.ietf.org/html/rfc1.html']" "[@class='rfc reference external']/strong", 'RFC 1'), + (".//a[@href='http://tools.ietf.org/html/rfc1.html']" + "[@class='rfc reference external']/strong", 'Request for Comments #1'), (".//a[@href='objects.html#envvar-HOME']" "[@class='reference internal']/code/span[@class='pre']", 'HOME'), (".//a[@href='#with']" From 95fc3ce88817658db79cdc8b278e3fef42bcdb98 Mon Sep 17 00:00:00 2001 From: tk0miya Date: Wed, 20 Aug 2014 12:21:18 +0900 Subject: [PATCH 3/3] Backed out changeset 177fce4ba59c --- sphinx/roles.py | 34 ++++++++++++++-------------------- tests/root/markup.txt | 2 -- tests/test_build_html.py | 4 ---- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/sphinx/roles.py b/sphinx/roles.py index aaf6272bd..122c52853 100644 --- a/sphinx/roles.py +++ b/sphinx/roles.py @@ -158,7 +158,7 @@ class XRefRole(object): return [node], [] -def indexmarkup_role(typ, rawtext, text, lineno, inliner, +def indexmarkup_role(typ, rawtext, etext, lineno, inliner, options={}, content=[]): """Role for PEP/RFC references that generate an index entry.""" env = inliner.document.settings.env @@ -166,53 +166,47 @@ def indexmarkup_role(typ, rawtext, text, lineno, inliner, typ = env.config.default_role else: typ = typ.lower() - has_explicit_title, title, target = split_explicit_title(text) - title = utils.unescape(title) - target = utils.unescape(target) + text = utils.unescape(etext) targetid = 'index-%s' % env.new_serialno('index') indexnode = addnodes.index() targetnode = nodes.target('', '', ids=[targetid]) inliner.document.note_explicit_target(targetnode) if typ == 'pep': indexnode['entries'] = [ - ('single', _('Python Enhancement Proposals; PEP %s') % target, + ('single', _('Python Enhancement Proposals; PEP %s') % text, targetid, '')] anchor = '' - anchorindex = target.find('#') + anchorindex = text.find('#') if anchorindex > 0: - target, anchor = target[:anchorindex], target[anchorindex:] - if not has_explicit_title: - title = "PEP " + utils.unescape(title) + text, anchor = text[:anchorindex], text[anchorindex:] try: - pepnum = int(target) + pepnum = int(text) except ValueError: - msg = inliner.reporter.error('invalid PEP number %s' % target, + msg = inliner.reporter.error('invalid PEP number %s' % text, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] ref = inliner.document.settings.pep_base_url + 'pep-%04d' % pepnum - sn = nodes.strong(title, title) + sn = nodes.strong('PEP '+text, 'PEP '+text) rn = nodes.reference('', '', internal=False, refuri=ref+anchor, classes=[typ]) rn += sn return [indexnode, targetnode, rn], [] elif typ == 'rfc': - indexnode['entries'] = [('single', 'RFC; RFC %s' % target, targetid, '')] + indexnode['entries'] = [('single', 'RFC; RFC %s' % text, targetid, '')] anchor = '' - anchorindex = target.find('#') + anchorindex = text.find('#') if anchorindex > 0: - target, anchor = target[:anchorindex], target[anchorindex:] - if not has_explicit_title: - title = "RFC " + utils.unescape(title) + text, anchor = text[:anchorindex], text[anchorindex:] try: - rfcnum = int(target) + rfcnum = int(text) except ValueError: - msg = inliner.reporter.error('invalid RFC number %s' % target, + msg = inliner.reporter.error('invalid RFC number %s' % text, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum - sn = nodes.strong(title, title) + sn = nodes.strong('RFC '+text, 'RFC '+text) rn = nodes.reference('', '', internal=False, refuri=ref+anchor, classes=[typ]) rn += sn diff --git a/tests/root/markup.txt b/tests/root/markup.txt index f6f955e24..7ce721bab 100644 --- a/tests/root/markup.txt +++ b/tests/root/markup.txt @@ -132,9 +132,7 @@ Adding \n to test unescaping. *Linking inline markup* * :pep:`8` -* :pep:`Python Enhancement Proposal #8 <8>` * :rfc:`1` -* :rfc:`Request for Comments #1 <1>` * :envvar:`HOME` * :keyword:`with` * :token:`try statement ` diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 17a09eae9..d13c7ac6f 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -129,12 +129,8 @@ HTML_XPATH = { (".//li/code/em/span[@class='pre']", '^i$'), (".//a[@href='http://www.python.org/dev/peps/pep-0008']" "[@class='pep reference external']/strong", 'PEP 8'), - (".//a[@href='http://www.python.org/dev/peps/pep-0008']" - "[@class='pep reference external']/strong", 'Python Enhancement Proposal #8'), (".//a[@href='http://tools.ietf.org/html/rfc1.html']" "[@class='rfc reference external']/strong", 'RFC 1'), - (".//a[@href='http://tools.ietf.org/html/rfc1.html']" - "[@class='rfc reference external']/strong", 'Request for Comments #1'), (".//a[@href='objects.html#envvar-HOME']" "[@class='reference internal']/code/span[@class='pre']", 'HOME'), (".//a[@href='#with']"