def test_LiteralIncludeReader_start_after(literal_inc_path): options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" pass\n" "\n") assert reader.lineno_start == 7
def test_LiteralIncludeReader_tabwidth(testroot): # tab-width: 4 options = {'tab-width': 4, 'pyobject': 'Qux'} reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Qux:\n" " def quux(self):\n" " pass\n") # tab-width: 8 options = {'tab-width': 8, 'pyobject': 'Qux'} reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Qux:\n" " def quux(self):\n" " pass\n")
def test_LiteralIncludeReader_lineno_start(): options = {'lineno-start': 5} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == LITERAL_INC_PATH.text() assert lines == 14 assert reader.lineno_start == 5
def test_LiteralIncludeReader_prepend(literal_inc_path): options = {'lines': '1', 'prepend': 'Hello', 'append': 'Sphinx'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("Hello\n" "# Literally included file using Python highlighting\n" "Sphinx\n")
def test_LiteralIncludeReader_lines2(literal_inc_path): options = {'lines': '1,4,6'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (u"# Literally included file using Python highlighting\n" u"foo = \"Including Unicode characters: üöä\"\n" u"class Foo:\n")
def test_LiteralIncludeReader_pyobject1(literal_inc_path): options = {'lineno-match': True, 'pyobject': 'Foo'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Foo:\n" " pass\n") assert reader.lineno_start == 6
def test_LiteralIncludeReader_lineno_start(literal_inc_path): options = {'lineno-start': 5} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == literal_inc_path.text() assert lines == 14 assert reader.lineno_start == 5
def test_LiteralIncludeReader_lines1(): options = {'lines': '1-4'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (u"# Literally included file using Python highlighting\n" u"# -*- coding: utf-8 -*-\n" u"\n" u"foo = \"Including Unicode characters: üöä\"\n")
def test_LiteralIncludeReader_lines_and_lineno_match1(literal_inc_path): options = {'lines': '4-6', 'lineno-match': True} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (u"foo = \"Including Unicode characters: üöä\"\n" u"\n" u"class Foo:\n") assert reader.lineno_start == 4
def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path): options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("\n" "class Foo:\n" "\n") assert reader.lineno_start == 1
def test_LiteralIncludeReader_pyobject2(literal_inc_path): options = {'pyobject': 'Bar'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Bar:\n" " def baz():\n" " pass\n") assert reader.lineno_start == 1 # no lineno-match
def test_LiteralIncludeReader_start_at(): options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Foo:\n" " pass\n" "\n" "class Bar:\n") assert reader.lineno_start == 6
def test_LiteralIncludeReader_start_after_and_lines(literal_inc_path): options = {'lineno-match': True, 'lines': '6-', 'start-after': 'coding', 'end-before': 'comment'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("\n" "class Bar:\n" " def baz():\n" " pass\n" "\n") assert reader.lineno_start == 8
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path): options = {'start-at': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'end-at': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'start-after': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'end-before': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()
def test_LiteralIncludeReader_start_after_and_lines(literal_inc_path): options = {'lineno-match': True, 'lines': '6-', 'start-after': 'Literally', 'end-before': 'comment'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("\n" "class Bar:\n" " def baz():\n" " pass\n" "\n") assert reader.lineno_start == 7
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path): options = {'start-at': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'end-at': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'start-after': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read() options = {'end-before': 'NOTHING'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()
def run(self): location = self.state_machine.get_source_and_line(self.lineno) rel_filename, filename = self.env.relfn2path(self.arguments[0]) self.env.note_dependency(rel_filename) reader = LiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read(location=location) node = button_node() node['text'] = "Try online" node['link'] = "http://liquidity-lang.org/edit?source="+urllib.parse.quote_plus(text) return [node]
def run(self): document = self.state.document if not document.settings.file_insertion_enabled: return [ document.reporter.warning("File insertion disabled", line=self.lineno) ] # convert options['diff'] to absolute path if "diff" in self.options: _, path = self.env.relfn2path(self.options["diff"]) self.options["diff"] = path try: location = self.state_machine.get_source_and_line(self.lineno) rel_filename, filename = self.env.relfn2path(self.arguments[0]) self.env.note_dependency(rel_filename) reader = LiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read(location=location) retnode = nodes.literal_block(text, text, source=filename) set_source_info(self, retnode) if self.options.get("diff"): # if diff is set, set udiff retnode["language"] = "udiff" elif "language" in self.options: retnode["language"] = self.options["language"] retnode["linenos"] = ("linenos" in self.options or "lineno-start" in self.options or "lineno-match" in self.options) retnode["classes"] += self.options.get("class", []) extra_args = retnode["highlight_args"] = {} if "emphasize-lines" in self.options: hl_lines = parselinenos(self.options["emphasize-lines"], lines) if any(i >= lines for i in hl_lines): logger.warning( "line number spec is out of range(1-%d): %r", lines, self.options["emphasize-lines"]) extra_args["hl_lines"] = [x + 1 for x in hl_lines if x < lines] extra_args["linenostart"] = reader.lineno_start container_node = nodes.container("", literal_block=True, classes=["example-block-wrapper"]) container_node += example_header(filename=filename) container_node += retnode retnode = container_node return [retnode] except Exception as exc: return [ document.reporter.warning(text_type(exc), line=self.lineno) ]
def test_LiteralIncludeReader_dedent(literal_inc_path): # dedent: 2 options = {'lines': '10-12', 'dedent': 2} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" def baz():\n" " pass\n" "\n") # dedent: 4 options = {'lines': '10-12', 'dedent': 4} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("def baz():\n" " pass\n" "\n") # dedent: 6 options = {'lines': '10-12', 'dedent': 6} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("f baz():\n" " pass\n" "\n")
def test_LiteralIncludeReader_dedent_and_append_and_prepend(literal_inc_path): # dedent: 2 options = { 'lines': '9-11', 'dedent': 2, 'prepend': 'class Foo:', 'append': '# comment' } reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Foo:\n" " def baz():\n" " pass\n" "\n" "# comment\n")
def test_LiteralIncludeReader_diff(): options = {'diff': TESTROOT_PATH / 'literal-diff.inc'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("--- " + TESTROOT_PATH + "/literal-diff.inc\n" "+++ " + TESTROOT_PATH + "/literal.inc\n" "@@ -7,8 +7,8 @@\n" " pass\n" " \n" " class Bar:\n" "- def baz(self):\n" "+ def baz():\n" " pass\n" " \n" "-# comment after Bar class\n" "+# comment after Bar class definition\n" " def bar(): pass\n")
def test_LiteralIncludeReader_diff(testroot, literal_inc_path): options = {'diff': testroot / 'literal-diff.inc'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("--- " + testroot + "/literal-diff.inc\n" "+++ " + testroot + "/literal.inc\n" "@@ -6,8 +6,8 @@\n" " pass\n" " \n" " class Bar:\n" "- def baz(self):\n" "+ def baz():\n" " pass\n" " \n" "-# comment after Bar class\n" "+# comment after Bar class definition\n" " def bar(): pass\n")
def test_LiteralIncludeReader_diff(testroot, literal_inc_path): options = {'diff': testroot / 'literal-diff.inc'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("--- " + testroot + "/literal-diff.inc\n" "+++ " + testroot + "/literal.inc\n" "@@ -7,8 +7,8 @@\n" " pass\n" " \n" " class Bar:\n" "- def baz(self):\n" "+ def baz():\n" " pass\n" " \n" "-# comment after Bar class\n" "+# comment after Bar class definition\n" " def bar(): pass\n")
def test_LiteralIncludeReader_diff(): options = {'diff': TESTROOT_PATH / 'literal-diff.inc'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("--- " + TESTROOT_PATH + "/literal-diff.inc\n" "+++ " + TESTROOT_PATH + "/literal.inc\n" "@@ -7,8 +7,8 @@\n" " pass\n" " \n" " class Bar:\n" "- def baz(self):\n" "+ def baz():\n" " pass\n" " \n" "-# comment after Bar class\n" "+# comment after Bar class definition\n" " def bar(): pass\n")
def run(self) -> List[Node]: document = self.state.document try: location = self.state_machine.get_source_and_line(self.lineno) rel_filename, filename = self.env.relfn2path(self.arguments[0]) self.env.note_dependency(rel_filename) reader = LiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read(location=location) text = text.split("\n---\n")[int(self.options['section'])] retnode = nodes.literal_block( text, text, source=filename, language=self.options['language']) # type: Element return [retnode] except Exception as exc: return [document.reporter.warning(exc, line=self.lineno)]
def attach_literal_node(path): from sphinx.directives.code import LiteralIncludeReader from sphinx.util.nodes import set_source_info nonlocal protocol if path.suffix == '.txt': # <literal_block highlight_args="{'linenostart': 1}" # linenos="False" # source="/home/kale/research/projects/201904_bind_dna/notebook/20190604_dnase_pick_qpcr_primers/20190604_pcr.txt" # xml:space="preserve"> # ... # From `sphinx/directives/code.py`: env = self.state.document.settings.env location = self.state_machine.get_source_and_line(self.lineno) rel_filename, filename = env.relfn2path(str(path)) env.note_dependency(rel_filename) reader = LiteralIncludeReader(filename, self.options, env.config) text, lines = reader.read(location=location) literal_node = nodes.literal_block(text, text, source=filename) set_source_info(self, literal_node) protocol += [literal_node] else: from sphinx.roles import specific_docroles protocol += specific_docroles['download']( 'download', rawtext=str(path), text=str(path), lineno=self.lineno, inliner=self.state.inliner, )[0]
def test_LiteralIncludeReader_tabwidth_dedent(testroot): options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'} reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("def quux(self):\n" " pass\n")
def test_LiteralIncludeReader_end_before(literal_inc_path): options = {'end-before': 'nclud'} # *nclud* matches first and third lines. reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("# Literally included file using Python highlighting\n" "\n")
def test_LiteralIncludeReader_lines_and_lineno_match3(literal_inc_path, app, status, warning): options = {'lines': '100-', 'lineno-match': True} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()
def test_LiteralIncludeReader_pyobject_and_lines(literal_inc_path): options = {'pyobject': 'Bar', 'lines': '2-'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" def baz():\n" " pass\n")
def test_LiteralIncludeReader_lines_and_lineno_match2(app, status, warning): options = {'lines': '1,4,6', 'lineno-match': True} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()
def test_LiteralIncludeReader_lines_and_lineno_match2(app, status, warning): options = {'lines': '1,4,6', 'lineno-match': True} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()
def test_LiteralIncludeReader_tabwidth_dedent(testroot): options = {'tab-width': 4, 'dedent': 4, 'pyobject': 'Qux.quux'} reader = LiteralIncludeReader(testroot / 'target.py', options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("def quux(self):\n" " pass\n")
def test_LiteralIncludeReader_pyobject3(): options = {'pyobject': 'Bar.baz'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" def baz():\n" " pass\n")
def test_LiteralIncludeReader_start_after(literal_inc_path): options = {'lineno-match': True, 'start-after': 'Foo', 'end-before': 'Bar'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" pass\n" "\n") assert reader.lineno_start == 6
def run(self) -> List[Node]: if 'fragment' in self.options: self.options['start-after'] = self.options['fragment'] self.options['end-before'] = self.options['fragment'] document = self.state.document if not document.settings.file_insertion_enabled: return [ document.reporter.warning('File insertion disabled', line=self.lineno) ] # convert options['diff'] to absolute path if 'diff' in self.options: _, path = self.env.relfn2path(self.options['diff']) self.options['diff'] = path try: location = self.state_machine.get_source_and_line(self.lineno) doxygen_snippet_root = self.config.html_context.get( 'doxygen_snippet_root') if doxygen_snippet_root and os.path.exists(doxygen_snippet_root): rel_filename = self.arguments[0] filename = os.path.join(doxygen_snippet_root, rel_filename) else: rel_filename, filename = self.env.relfn2path(self.arguments[0]) self.env.note_dependency(rel_filename) reader = LiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read(location=location) retnode = nodes.literal_block(text, text, source=filename) # type: Element retnode['force'] = 'force' in self.options self.set_source_info(retnode) if self.options.get('diff'): # if diff is set, set udiff retnode['language'] = 'udiff' elif 'language' in self.options: retnode['language'] = self.options['language'] if ('linenos' in self.options or 'lineno-start' in self.options or 'lineno-match' in self.options): retnode['linenos'] = True retnode['classes'] += self.options.get('class', []) extra_args = retnode['highlight_args'] = {} if 'emphasize-lines' in self.options: hl_lines = parselinenos(self.options['emphasize-lines'], lines) if any(i >= lines for i in hl_lines): logger.warning( __('line number spec is out of range(1-%d): %r') % (lines, self.options['emphasize-lines']), location=location) extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines] extra_args['linenostart'] = reader.lineno_start if 'caption' in self.options: caption = self.options['caption'] or self.arguments[0] retnode = container_wrapper(self, retnode, caption) # retnode will be note_implicit_target that is linked from caption and numref. # when options['name'] is provided, it should be primary ID. self.add_name(retnode) return [retnode] except Exception as exc: return [document.reporter.warning(exc, line=self.lineno)]
def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path): options = {'lines': '2, 3, 5', 'start-at': 'foo', 'end-before': '#'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("\n" "class Foo:\n" "\n") assert reader.lineno_start == 1
def test_LiteralIncludeReader_pyobject1(literal_inc_path): options = {'lineno-match': True, 'pyobject': 'Foo'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Foo:\n" " pass\n") assert reader.lineno_start == 5
def test_LiteralIncludeReader_pyobject2(literal_inc_path): options = {'pyobject': 'Bar'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Bar:\n" " def baz():\n" " pass\n") assert reader.lineno_start == 1 # no lineno-match
def test_LiteralIncludeReader_pyobject3(): options = {'pyobject': 'Bar.baz'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" def baz():\n" " pass\n")
def test_LiteralIncludeReader_start_at(): options = {'lineno-match': True, 'start-at': 'Foo', 'end-at': 'Bar'} reader = LiteralIncludeReader(LITERAL_INC_PATH, options, DUMMY_CONFIG) content, lines = reader.read() assert content == ("class Foo:\n" " pass\n" "\n" "class Bar:\n") assert reader.lineno_start == 6
def test_LiteralIncludeReader_pyobject_and_lines(literal_inc_path): options = {'pyobject': 'Bar', 'lines': '2-'} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) content, lines = reader.read() assert content == (" def baz():\n" " pass\n")
def test_LiteralIncludeReader_lines_and_lineno_match3(literal_inc_path, app, status, warning): options = {'lines': '100-', 'lineno-match': True} reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG) with pytest.raises(ValueError): content, lines = reader.read()