Пример #1
0
    def run(self):

        self.assert_has_content()

        save = sys.stdout
        try:

            # Get current file
            current_rst_file = self.state_machine.input_lines.source(
                self.lineno - self.state_machine.input_offset - 1)

            args = [x for x in self.content if x != ""][0].split(" ")
            name = args.pop(0)

            module = import_module("..%s" % (name.replace("-", "_"), ),
                                   package=__name__)

            sys.stdout = StringIO()
            module.execute(*args)
            out = sys.stdout.getvalue()

            # Parse output
            rst_lines = statemachine.string2lines(out)
            # Insert in place
            self.state_machine.insert_input(rst_lines, current_rst_file)

        except Exception as e:
            # rst_lines = statemachine.string2lines(str(e))
            rst_lines = statemachine.string2lines(name + "\n" +
                                                  traceback.format_exc())
            self.state_machine.insert_input(rst_lines, current_rst_file)
        finally:
            sys.stdout = save
        return []
Пример #2
0
    def run(self):

        self.assert_has_content()

        here = os.getcwd()

        try:

            # Get current file
            current_rst_file = self.state_machine.input_lines.source(
                self.lineno - self.state_machine.input_offset - 1)

            os.chdir(os.path.dirname(current_rst_file))

            cmd = [x for x in self.content if x != ""][0]

            out = subprocess.check_output(split(cmd)).decode("utf-8")

            # Parse output
            rst_lines = statemachine.string2lines(out)
            # Insert in place
            self.state_machine.insert_input(rst_lines, current_rst_file)

        except Exception:
            # rst_lines = statemachine.string2lines(str(e))
            rst_lines = statemachine.string2lines(traceback.format_exc())
            self.state_machine.insert_input(rst_lines, current_rst_file)

        finally:
            os.chdir(here)

        return []
Пример #3
0
    def run(self):
        env = self.state.document.settings.env
        package = env.temp_data.get('el:package')
        keymap_list = DATA.get(package, {}).get('keymap', [])
        keymap_name = self.arguments[0]
        for keymap in keymap_list:
            if keymap['name'] == keymap_name:
                break
        else:
            return [
                self.state.reporter.warning(
                    "Keymap {0} not found".format(keymap_name))
            ]
        nodelist = []

        mapdoc = keymap['doc']
        if mapdoc:
            nd = nodes.paragraph()
            lines = string2lines(doc_to_rst(mapdoc))
            if lines and lines[-1].startswith('(fn '):
                lines = lines[:-1]
            self.state.nested_parse(StringList(lines), 0, nd)
            nodelist.append(nd)

        exclude = self.options.get('exclude', [])
        replace = self.options.get('replace', [])
        for keybind in filter_by_exclude_regexp_list(keymap['data'], exclude,
                                                     lambda x: x['func']):
            desc = addnodes.desc()
            desc['domain'] = 'el'
            desc['objtype'] = 'keybind'
            desc['noindex'] = False
            signode = addnodes.desc_signature()
            # signode += addnodes.desc_annotation("", 'keybind ')
            key = simple_sed(replace, keybind['key'])
            signode += addnodes.desc_name("", key)
            signode += addnodes.desc_addname("", " " + keybind['func'])
            desc += signode
            if keybind['doc']:
                nd = addnodes.desc_content()
                lines = string2lines(doc_to_rst(keybind['doc']))
                if lines and lines[-1].startswith('(fn '):
                    lines = lines[:-1]
                self.state.nested_parse(StringList(lines), 0, nd)
                desc += nodes.definition("", nd)
            nodelist.append(desc)

        return nodelist
Пример #4
0
    def exec_to_state_machine(self, code, tab_width=None):
        oldStdout, sys.stdout = sys.stdout, StringIO()

        if tab_width is None:
            # use default if not given as an option
            tab_width = self.state.document.settings.tab_width

        # get the path to this rST source file
        #   for inserting directly to state_machine
        source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
        try:
            exec("\n".join(code))

            # convert the multi-line string from stdout
            #   into a list of single-line strings
            lines = statemachine.string2lines(sys.stdout.getvalue(), tab_width, convert_whitespace=True)

            # insert the list of strings at the source
            #   of the original directive call
            self.state_machine.insert_input(lines, source)

            return []
        except Exception:
            document = self.state.document
            error_src = "Unable to execute python code at %s:%d:" % (basename(source), self.lineno)
            trace = "\n".join(traceback.format_exception(*sys.exc_info()))
            return [
                nodes.error(None, nodes.paragraph(text=error_src), nodes.literal_block(text=trace)),
                document.reporter.error("problem executing python code\n" "-- traceback included in document"),
            ]
        finally:
            sys.stdout = oldStdout
Пример #5
0
    def run(self):
        old_stdout, sys.stdout = sys.stdout, StringIO()

        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)

        try:
            exec('\n'.join(self.content), globals())
            text = sys.stdout.getvalue()
            lines = statemachine.string2lines(text,
                                              tab_width,
                                              convert_whitespace=True)
            self.state_machine.insert_input(lines, source)
            return []
        except Exception:
            return [
                nodes.error(
                    None,
                    nodes.paragraph(
                        text="Unable to execute python code at %s:%d:" %
                        (basename(source), self.lineno)),
                    nodes.paragraph(text=str(sys.exc_info()[1])))
            ]
        finally:
            sys.stdout = old_stdout
Пример #6
0
    def run(self):
        language = self.arguments[0]

        first_line = [self.arguments[1]] if len(self.arguments) > 1 else []
        contents = u'\n'.join(first_line + list(self.content))
        defined_in = self.state.document.current_source
        source_path = self.state.document.get('source')

        try:
            data = yaml.safe_load(contents)
            if not isinstance(data, dict):
                raise self.severe(u'Expected YAML dictionary')

            code = data.get('code', '')
        except (yaml.scanner.ScannerError, yaml.parser.ParserError) as error:
            raise self.severe(u'Error parsing YAML:\n{}.'.format(ErrorString(error)))

        if not code:
            return []

        code = '\n'.join(['.. code-block:: javascript\n'] +
            ['   ' + line for line in code.split('\n')])

        rendered_lines = statemachine.string2lines(
            code, 4, convert_whitespace=1)
        self.state_machine.insert_input(rendered_lines, '')

        return []
Пример #7
0
 def run(self):
     result = super(ELSExp, self).run()
     if "nodoc" not in self.options:
         package = self.env.temp_data.get('el:package')
         node = addnodes.desc_content()
         string = DATA_DOC_STRINGS.get(package, {}) \
                                  .get(self.names[0][1], "")
         lines = string2lines(string)
         if lines and lines[-1].startswith('(fn '):
             lines = lines[:-1]
         self.state.nested_parse(StringList(lines), 0, node)
         if (result[1][1].children
                 and isinstance(result[1][1][0], nodes.field_list)):
             cresult = result[1][1].deepcopy()
             target = result[1][1]
             target.clear()
             target.append(cresult[0])
             target.extend(node)
             target.extend(cresult[1:])
         else:
             cresult = result[1][1].deepcopy()
             target = result[1][1]
             target.clear()
             target.extend(node)
             target.extend(cresult)
     return result
Пример #8
0
    def run(self):
        # Respect the same disabling options as the `raw` directive
        if (not self.state.document.settings.raw_enabled
                or not self.state.document.settings.file_insertion_enabled):
            raise self.warning('"%s" directive disabled.' % self.name)

        # Retrieve the backreferences directory
        config = self.state.document.settings.env.config
        backreferences_dir = config.sphinx_gallery_conf['backreferences_dir']

        # Parse the argument into the individual objects
        obj_list = self.arguments[0].split()

        lines = []

        # Add a heading if requested
        if 'add-heading' in self.options:
            heading = self.options['add-heading']
            if heading == "":
                if len(obj_list) == 1:
                    heading = 'Examples using ``{}``'.format(obj_list[0])
                else:
                    heading = 'Examples using one of multiple objects'
            lines.append(heading)
            heading_level = self.options.get('heading-level', '^')
            lines.append(heading_level * len(heading))

        def has_backrefs(obj):
            src_dir = config.sphinx_gallery_conf['src_dir']
            path = os.path.join(src_dir, backreferences_dir,
                                '{}.examples'.format(obj))
            return os.path.isfile(path) and os.path.getsize(path) > 0

        if not any(has_backrefs(obj) for obj in obj_list):
            return []

        # Add div containing all thumbnails;
        # this is helpful for controlling grid or flexbox behaviours
        lines.append(THUMBNAIL_PARENT_DIV)

        # Insert the backreferences file(s) using the `include` directive
        for obj in obj_list:
            path = os.path.join(
                '/',  # Sphinx treats this as the source dir
                backreferences_dir,
                '{}.examples'.format(obj))

            # Always remove the heading (first 5 lines) from the file
            lines.append('.. include:: {}\n    :start-line: 5'.format(path))

        # Close thumbnail parent div
        lines.append(THUMBNAIL_PARENT_DIV_CLOSE)

        # Parse the assembly of `include` and `raw` directives
        text = '\n'.join(lines)
        include_lines = statemachine.string2lines(text,
                                                  convert_whitespace=True)
        self.state_machine.insert_input(include_lines, path)

        return []
Пример #9
0
    def run(self):
        buf = []
        want = self.content[0].split('.')
        if len(want) is 1:
            thing = __import__('.'.join(want))
        else:
            buf = []
            mod = __import__('.'.join(want[:-1]), globals(), locals(),
                             want[-1], 0)
            thing = getattr(mod, want[-1])
            if not isinstance(thing, type):
                for line in inspect.getsource(mod).split('\n'):
                    if line[:line.find('=')].strip() == want[-1]:
                        buf.append(line)
                    elif buf:
                        buf.append(line)
                        if line == "":
                            break

        if not buf:
            buf = inspect.getsource(thing).split('\n')

        template = ["", ".. code-block:: python", ""
                    ] + ["    {0}".format(line) for line in buf]

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        lines = statemachine.string2lines('\n'.join(template),
                                          tab_width,
                                          convert_whitespace=True)
        self.state_machine.insert_input(lines, source)
        return []
Пример #10
0
def specializer_xref(symbol, sexp, state, package, node_type=nodes.inline):
    result = StringIO()
    first = True
    for atom in sexp:
        if first:
            first = False
        else:
            result.write(" ")

        if atom.startswith("KEYWORD:"):
            result.write("(EQL :%s)" % atom.split(":")[-1])
        elif package:
            if atom.startswith(package + ":"):
                result.write(atom.split(":")[-1])
            else:
                result.write(atom)
        else:
            result.write(atom)

    target = " ".join([a.lower() for a in sexp])
    node = node_type()
    result.seek(0)
    xref = ":cl:method:`(%s) <%s %s>`" % \
           (result.read().lower(), symbol, target)
    lines = string2lines(xref)
    state.nested_parse(StringList(lines), 0, node)
    return node
Пример #11
0
    def run(self):
        want = self.content[0]
        parts = want.split('.')
        template = []

        if len(parts) is 1:
            thing = __import__(want)
        else:
            thing = getattr(
                __import__('.'.join(parts[:-1]), globals(), locals(),
                           [parts[-1]], 0), parts[-1])

        for field in sorted(thing.fields):
            options = thing.fields[field]
            if type(options) is tuple:
                template.extend([field] + [
                    "    {0}".format(line)
                    for line in dedent(options[0]).strip().split('\n')
                ] + [""])

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        lines = statemachine.string2lines('\n'.join(template),
                                          tab_width,
                                          convert_whitespace=True)
        self.state_machine.insert_input(lines, source)
        return []
Пример #12
0
    def run(self):
        """Generate the list and make symlinks"""
        # Init
        env = self.state.document.settings.env
        versions = env.docversions_list
        current_html_dir = env.docversions_current_html_dir
        index = env.docversions_index_html
        verdir = env.docversions_subpath_versions
        all_dir = os.path.join(current_html_dir, verdir)
        if not os.path.exists(all_dir):
            os.makedirs(all_dir)

        # Links + rst
        rst_links = []
        for name, html_dir in versions:
            symlink = os.path.join(all_dir, name)
            if os.path.islink(symlink):
                os.remove(symlink)
            os.symlink(html_dir, os.path.join(all_dir, name))
            rst_links.append('`%(name)s <%(verdir)s/%(name)s/%(index)s>`_'%locals())
        versions = ', '.join(rst_links)
        raw_text = env.docversions_template%locals()

        # Insert
        source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
        include_lines = string2lines(raw_text, convert_whitespace=1)
        self.state_machine.insert_input(include_lines,source)

        return []
Пример #13
0
 def run(self):
     """
     Implements the directive
     """
     # Get content and options
     path = self.arguments[0]
     node = lux_extension()
     klass = self.options.get('classname') or 'Extension'
     try:
         module = import_module(path)
         Ext = getattr(module, klass)
         params = list(Ext.meta.config.values())
     except Exception as e:
         document = self.state.document
         return [
             document.reporter.warning(
                 'Could not import Extension from "%s". %s' % (path, e))
         ]
     tab_width = self.options.get('tab-width',
                                  self.state.document.settings.tab_width)
     rawdocs = '\n'.join(self.text(params))
     include_lines = statemachine.string2lines(rawdocs,
                                               tab_width,
                                               convert_whitespace=1)
     self.state_machine.insert_input(include_lines, 'lux_extension')
     return []
Пример #14
0
    def run(self):
        oldStdout, sys.stdout = sys.stdout, StringIO()

        tab_width = self.options.get("tab-width",
                                     self.state.document.settings.tab_width)
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)

        try:
            exec("\n".join(self.content))
            text = sys.stdout.getvalue()
            lines = statemachine.string2lines(text,
                                              tab_width,
                                              convert_whitespace=True)
            self.state_machine.insert_input(lines, source)
            return []
        except Exception:
            return [
                nodes.error(
                    None,
                    nodes.paragraph(
                        text="Unable to execute python "
                        "code at {file}:{line}".format(
                            file=os.path.basename(source), line=self.lineno)),
                    nodes.paragraph(text=str(sys.exc_info()[1])),
                )
            ]
        finally:
            sys.stdout = oldStdout
Пример #15
0
    def run(self):
        language = self.arguments[0]

        first_line = [self.arguments[1]] if len(self.arguments) > 1 else []
        contents = u'\n'.join(first_line + list(self.content))
        defined_in = self.state.document.current_source
        source_path = self.state.document.get('source')

        try:
            data = yaml.safe_load(contents)
            if not isinstance(data, dict):
                raise self.severe(u'Expected YAML dictionary')

            code = data.get('code', '')
        except (yaml.scanner.ScannerError, yaml.parser.ParserError) as error:
            raise self.severe(u'Error parsing YAML:\n{}.'.format(
                ErrorString(error)))

        if not code:
            return []

        code = '\n'.join(['.. code-block:: javascript\n'] +
                         ['   ' + line for line in code.split('\n')])

        rendered_lines = statemachine.string2lines(code,
                                                   4,
                                                   convert_whitespace=1)
        self.state_machine.insert_input(rendered_lines, '')

        return []
Пример #16
0
    def run(self) -> List[nodes.Node]:
        """Generate a node tree in place of the directive."""
        target_version = self.content[:1][0] if self.content[:1] else None
        if self.content[1:]:  # inner content present
            raise self.error(
                f'Error in "{self.name!s}" directive: '
                'only one argument permitted.',
            )

        config = self.state.document.settings.env.config  # noqa: WPS219
        autoversion_mode = config.towncrier_draft_autoversion_mode
        include_empty = config.towncrier_draft_include_empty

        try:
            draft_changes = _get_changelog_draft_entries(
                target_version or
                _get_draft_version_fallback(autoversion_mode, config),
                allow_empty=include_empty,
                working_dir=config.towncrier_draft_working_directory,
                config_path=config.towncrier_draft_config_path,
            )
        except subprocess.CalledProcessError as proc_exc:
            raise self.error(proc_exc)
        except LookupError:
            return []

        self.state_machine.insert_input(
            statemachine.string2lines(draft_changes),
            '[towncrier draft]',
        )
        return []
Пример #17
0
        def run(self):
            source_path = self.state.document.get('source')
            if defined_in != BUILT_IN_PATH and (
                    source_path not in REGISTERED
                    or name not in REGISTERED[source_path]):
                raise self.severe(u'Unknown directive: {}. '
                                  u'Try including {}'.format(
                                      self.name, defined_in))

            contents = '\n'.join(self.content)

            if is_yaml:
                data = self.process_yaml(contents)
            else:
                title = self.arguments[0]
                data = {'directive': name, 'body': contents, 'title': title}

            try:
                rendered = template.render(data)
            except Exception as error:
                raise self.severe('Failed to render template: {}'.format(
                    ErrorString(error)))

            rendered_lines = statemachine.string2lines(rendered,
                                                       4,
                                                       convert_whitespace=1)
            self.state_machine.insert_input(rendered_lines, '')

            return []
Пример #18
0
 def run(self):
     tab_width = self.options.get("tab-width",
                                  self.state.document.settings.tab_width)
     source = self.state_machine.input_lines.source(
         self.lineno - self.state_machine.input_offset - 1)
     try:
         lines = []
         dest_dir = Path(source).parent
         for name in narratives.__all__:
             narrative = getattr(narratives, name)
             file_name = f"{narrative.name.replace(' ', '_')}.json"
             with open(dest_dir / file_name, "w") as n_file:
                 n_file.write(
                     json.dumps(narrative.json(), indent=4, sort_keys=True))
             lines.extend(
                 statemachine.string2lines(
                     TMPL.format(source_path=file_name),
                     tab_width,
                     convert_whitespace=True,
                 ))
         self.state_machine.insert_input(lines, source)
         return []
     except Exception:
         return [
             nodes.error(
                 None,
                 nodes.paragraph(
                     text=
                     f"Failed to produce ert_narratives in {basename(source)}:{self.lineno}:"
                 ),
                 nodes.paragraph(text=str(sys.exc_info()[1])),
             )
         ]
Пример #19
0
    def run(self):
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))

        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        img_folder = None
        img_folder_os = None
        if "image_folder" in self.options:
            # This is extremly messy..
            # To be able to test if file exist in path we need to use img_path_os
            # But that cannot be used for the .. image:: tag, instead we need to use the raw option!
            img_folder_os = os.path.normpath(
                os.path.join(source_dir, self.options["image_folder"]))
            img_folder = self.options["image_folder"]
        rawtext = parse_examples(path, img_folder, img_folder_os)

        include_lines = statemachine.string2lines(
            rawtext,
            self.state.document.settings.tab_width,
            convert_whitespace=True)

        self.state_machine.insert_input(include_lines, path)
        return []
Пример #20
0
    def run(self):
        """Generate the list and make symlinks"""
        # Init
        env = self.state.document.settings.env
        versions = env.docversions_list
        current_html_dir = env.docversions_current_html_dir
        index = env.docversions_index_html
        verdir = env.docversions_subpath_versions
        all_dir = os.path.join(current_html_dir, verdir)
        if not os.path.exists(all_dir):
            os.makedirs(all_dir)

        # Links + rst
        rst_links = []
        for name, html_dir in versions:
            symlink = os.path.join(all_dir, name)
            if os.path.islink(symlink):
                os.remove(symlink)
            os.symlink(html_dir, os.path.join(all_dir, name))
            rst_links.append('`%(name)s <%(verdir)s/%(name)s/%(index)s>`_' %
                             locals())
        versions = ', '.join(rst_links)
        raw_text = env.docversions_template % locals()

        # Insert
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        include_lines = string2lines(raw_text, convert_whitespace=1)
        self.state_machine.insert_input(include_lines, source)

        return []
Пример #21
0
    def __call__(self, obj):
        assert isinstance(obj, robot.parsing.model.UserKeyword)

        used_title_styles = self.context.state.memo.title_styles
        section_level = self.context.state.memo.section_level + 1
        title_style = get_title_style(used_title_styles, section_level)
        title = obj.name + '\n' + title_style * len(obj.name) + '\n\n'
        documentation = obj.doc.value.replace('\\n', '\n')  # fix linebreaks

        temp = nodes.Element()
        lines = statemachine.string2lines(title + documentation)
        self.context.content.data = lines
        self.context.state.nested_parse(
            self.context.content,
            self.context.content_offset,
            temp, match_titles=True
        )

        node = temp.children.pop()

        all_steps = filter(lambda x: not x.is_comment(), obj.steps)

        steps = nodes.literal_block()
        steps.extend(map(StepNode(self.context), all_steps))

        # Insert newlines between steps:
        for i in range(len(all_steps[:-1]), 0, -1):
            steps.insert(i, nodes.inline(text='\n'))
        node.append(steps)

        return node
Пример #22
0
    def run(self):
        self.reporter = self.state.document.reporter
        self.env = self.state.document.settings.env
        self.warnings = [] # type: List[unicode]

        try:
            source, lineno = self.reporter.get_source_and_line(self.lineno)
        except AttributeError:
            source = lineno = None
        logger.debug('[tlisp-autodoc] %s:%s: input:\n%s',
                     source, lineno, self.block_text)

        # Get environment
        tlispfuncs = self.env.config._tlispfuncs
        if tlispfuncs is None:
            return []

        # Look for the requested object
        objname = self.arguments[0].lower()
        
        if objname not in tlispfuncs.functions:
            self.state_machine.reporter.warning('Unknown function name: %s'%objname, line=self.lineno)

        obj = tlispfuncs.functions[objname]
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)

        include_lines = [obj.tlisp_signature] + string2lines(obj.__doc__, convert_whitespace=1)
        self.state_machine.insert_input(include_lines,source)

        return []
Пример #23
0
 def run(self):
     rendered = URIWRITER_TEMPLATE
     rendered_lines = statemachine.string2lines(rendered,
                                                4,
                                                convert_whitespace=1)
     self.state_machine.insert_input(rendered_lines, '')
     return []
Пример #24
0
    def run(self):
        template = []

        ps = []
        ps_no_label = []

        for plan in plans:
            name = plan_labels.get(plan, plan.__name__)
            sig = f'``"{name}"``' if plan in plan_labels else f"{name}(...)"
            if plan in plan_labels:
                ps.append((name, sig, plan))
            else:
                ps_no_label.append((name, sig, plan))

        ps = sorted(ps, key=lambda i: i[0])
        ps.extend(sorted(ps_no_label, key=lambda i: i[-1].__name__))

        for name, sig, plan in ps:
            template.append(f"* :ref:`{name} <plan_{name}>`")

        template.append("")

        for name, sig, plan in ps:
            template.extend(list(self.explain_plan(name, sig, plan)))
            template.append("")

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        tab_width = self.options.get("tab-width",
                                     self.state.document.settings.tab_width)
        lines = statemachine.string2lines("\n".join(template),
                                          tab_width,
                                          convert_whitespace=True)
        self.state_machine.insert_input(lines, source)
        return []
 def run (self):
     pth = self.courtPathFromJurisdiction(self.arguments[0])
     rawlines = ""
     ifh = open(pth)
     while 1:
         line = ifh.readline()
         if not line: break
         if nodes.whitespace_normalize_name(line).startswith(".. reporter-key::"):
             reporter_key = re.sub("\.\.\s+reporter-key::\s*","",line).strip()
             pth = self.reporterPathFromJurisdiction(self.arguments[0],reporter_key)
             newlines = open(pth).read()
             newlines = newlines.split("\n")
             for i in range(0,len(newlines),1):
                 newlines[i] = re.sub("^(\s*)(\.\.\s+reporter::.*)","\\1\\2\n\\1      :jurisdiction: %s" % self.arguments[0],newlines[i])
                 newlines[i] = "   " + newlines[i]
             newlines = "\n".join(newlines)
             rawlines += newlines
         else:
             rawlines += line
     tab_width = self.options.get(
         'tab-width', self.state.document.settings.tab_width)
     include_lines = statemachine.string2lines(rawlines, tab_width,
                                               convert_whitespace=True)
     self.state_machine.insert_input(include_lines, pth)
     return []
Пример #26
0
    def run(self):
        """Most of this method is from ``docutils.parser.rst.Directive``.

        docutils version: 0.12
        """
        if not self.state.document.settings.file_insertion_enabled:
            raise self.warning('"%s" directive disabled.' % self.name)
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = rst.directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        # get options (currently not use directive-specific options)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)

        # open the including file
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, ErrorString(error)))

        # read from the file
        startline = self.options.get('start-line', None)
        endline = self.options.get('end-line', None)
        try:
            if startline or (endline is not None):
                lines = include_file.readlines()
                rawtext = ''.join(lines[startline:endline])
            else:
                rawtext = include_file.read()
        except UnicodeError as error:
            raise self.severe('Problem with "%s" directive:\n%s' %
                              (self.name, ErrorString(error)))

        config = self.state.document.settings.env.config
        converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis,
                        parse_relative_links=config.m2r_parse_relative_links,
                        anonymous_references=config.m2r_anonymous_references,
                        disable_inline_math=config.m2r_disable_inline_math)
        include_lines = statemachine.string2lines(converter(rawtext),
                                                  tab_width,
                                                  convert_whitespace=True)
        self.state_machine.insert_input(include_lines, path)
        return []
Пример #27
0
    def run(self):
        env = self.state.document.settings.env
        rawdocs = '\n'.join(self.text())

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)

        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)

        if 'literal' in self.options:
            # Convert tabs to spaces, if `tab_width` is positive.
            if tab_width >= 0:
                text = rawtext.expandtabs(tab_width)
            else:
                text = rawtext
            literal_block = nodes.literal_block(rawtext, text, source=path)
            literal_block.line = 1
            return [literal_block]
        else:
            include_lines = statemachine.string2lines(rawdocs,
                                                      tab_width,
                                                      convert_whitespace=1)
            self.state_machine.insert_input(include_lines, targetid)
            return []
Пример #28
0
    def run(self):
        want = self.content[0]
        parts = want.split(".")

        if len(parts) == 1:
            thing = __import__(want)
        else:
            thing = getattr(
                __import__(".".join(parts[:-1]), globals(), locals(),
                           [parts[-1]], 0), parts[-1])

        if len(self.content) > 1:
            thing = getattr(thing, self.content[1])

        template = []
        for name in sorted(repr(t) for t in thing):
            template.append("``{0}``".format(name))

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        tab_width = self.options.get("tab-width",
                                     self.state.document.settings.tab_width)
        lines = statemachine.string2lines("\n".join(template),
                                          tab_width,
                                          convert_whitespace=True)
        self.state_machine.insert_input(lines, source)
        return []
Пример #29
0
def specializer(symbol, sexp, state, package, node_type=nodes.inline):
    result = StringIO()
    result.write("(")
    first = True
    for atom in sexp:
        if first:
            first = False
        else:
            result.write(" ")

        if atom.startswith("KEYWORD:"):
            result.write("(EQL :%s)" % atom.split(":")[-1])
        else:
            result.write(atom)
        result.write(" ")

    result.write(")")
    result.seek(0)

    xref = ':cl:generic:`%s <%s:%s>`' % \
           (result.read().lower(), package, symbol)
    lines = string2lines(xref)
    node = node_type()
    state.nested_parse(StringList(lines), 0, node)
    return node
Пример #30
0
    def _convert_content(self, text):
        list_lines = statemachine.string2lines(str_unicode(text))
        # Adding a source and line number to each line text warnings may appear when writing if there are issues with a line
        # if left None the warnings would be counted but don't appear in the output then you don't know the source of it
        items = [(self.state.document.current_source, self.lineno)] * len(list_lines)

        return statemachine.StringList(list_lines, items=items)
Пример #31
0
    def run(self):
        env = self.state.document.settings.env
        rawdocs = '\n'.join(self.text())

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)

        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)


        if 'literal' in self.options:
            # Convert tabs to spaces, if `tab_width` is positive.
            if tab_width >= 0:
                text = rawtext.expandtabs(tab_width)
            else:
                text = rawtext
            literal_block = nodes.literal_block(rawtext, text, source=path)
            literal_block.line = 1
            return [literal_block]
        else:
            include_lines = statemachine.string2lines(
                rawdocs, tab_width, convert_whitespace=1)
            self.state_machine.insert_input(include_lines, targetid)
            return []
Пример #32
0
def _format_description(ctx: click.Context) -> Iterator[str]:
	"""
	Format the description for a given `click.Command`.

	We parse this as reStructuredText, allowing users to embed rich
	information in their help messages if they so choose.
	"""

	help_string = ctx.command.help or ctx.command.short_help

	if help_string:
		help_string = inspect.cleandoc(help_string)
	else:
		return

	bar_enabled = False

	for line in statemachine.string2lines(help_string, tab_width=4, convert_whitespace=True):
		if line == '\x08':
			bar_enabled = True
			continue
		if line == '':
			bar_enabled = False

		if bar_enabled:
			yield f"| {line}"
		else:
			yield line

	yield ''
Пример #33
0
def get_content(obj):
    """Build a content block for parser consumption from an object."""
    source = inspect.getsourcefile(obj)
    srclines, srcstart = inspect.getsourcelines(obj)
    srcitems = [(source, i) for i in range(srcstart, srcstart + len(srclines))]

    doc = inspect.getdoc(obj) or ''
    doclines = statemachine.string2lines(doc)
    if doclines:
        docstart = srcstart
        indoc = False
        for add, line in enumerate(srclines):
            line = line.strip()
            if indoc:
                if line:
                    docstart += add
                    break
            elif line in ('"""', '"', "'''", "'"):
                indoc = True
                continue
            elif line and line[0] in ('"', "'"):
                docstart += add
                break

        docitems = [(source, i)
                    for i in range(docstart, docstart + len(doclines))]
    else:
        docitems = []

    return StringList(srclines, items=srcitems), StringList(doclines,
                                                            items=docitems)
Пример #34
0
def specializer(symbol, sexp, state, package, node_type=nodes.inline):
    result = StringIO()
    result.write("(")
    first = True
    for atom in sexp:
        if first:
            first = False
        else:
            result.write(" ")

        if atom.startswith("KEYWORD:"):
            result.write("(EQL :%s)" % atom.split(":")[-1])
        else:
            result.write(atom)
        result.write(" ")

    result.write(")")
    result.seek(0)

    xref = ':cl:generic:`%s <%s:%s>`' % \
           (result.read().lower(), package, symbol)
    lines = string2lines(xref)
    node = node_type()
    state.nested_parse(StringList(lines), 0, node)
    return node
Пример #35
0
def specializer_xref(symbol, sexp, state, package, node_type=nodes.inline):
    result = StringIO()
    first = True
    for atom in sexp:
        if first:
            first = False
        else:
            result.write(" ")

        if atom.startswith("KEYWORD:"):
            result.write("(EQL :%s)" % atom.split(":")[-1])
        elif package:
            if atom.startswith(package + ":"):
                result.write(atom.split(":")[-1])
            else:
                result.write(atom)
        else:
            result.write(atom)

    target = " ".join([a.lower() for a in sexp])
    node = node_type()
    result.seek(0)
    xref = ":cl:method:`(%s) <%s %s>`" % \
           (result.read().lower(), symbol, target)
    lines = string2lines(xref)
    state.nested_parse(StringList(lines), 0, node)
    return node
Пример #36
0
    def run(self):
        env = self.state.document.settings.env

        openapi_path = self.arguments[0]
        _, openapi_path = env.relfn2path(openapi_path)
        openapi_path = utils.relative_path(None, openapi_path)
        openapi_path = nodes.reprunicode(openapi_path)

        self.state.document.settings.record_dependencies.add(openapi_path)
        with open(openapi_path, 'r') as f:
            openapi = OpenAPI.load(f)

        try:
            rendered = OPENAPI_TEMPLATE.render({
                'tags': openapi.tags.values(),
                'servers': openapi.data['servers']
            })
        except Exception as error:
            raise self.severe('Failed to render template: {}'.format(ErrorString(error)))

        rendered_lines = statemachine.string2lines(rendered, 4, convert_whitespace=1)
        self.state_machine.insert_input(rendered_lines, '')

        # Allow people to use :ref: to link to resources. Sphinx offers two ways
        # of doing this: (1) lots of arcane boilerplate, or (2) hacking our way through.
        # Let's hope this doesn't break...
        stddomain = env.get_domain('std')
        labels = stddomain.data['labels']
        for method, path, methods in openapi.resources():
            method_hash = methods[method]['hash']
            if method_hash not in labels:
                labels[method_hash] = (env.docname, method_hash, '{} {}'.format(method.upper(), path))

        return []
Пример #37
0
    def run(self):
        template = [""]
        messages = protocol_register.message_register(1024)

        template.extend(list(self.messages_toc(messages)))
        template.append("")

        for kls in messages.message_classes:
            template.extend(list(self.message_group(kls)))

        title = "Message Objects"
        template.extend(
            ["", ".. _message_objects:", "", title, "-" * len(title), ""])
        template.extend(list(self.message_objects(messages)))

        title = "Enums"
        template.extend(
            ["", ".. _message_enums:", "", title, "-" * len(title), ""])
        template.extend(list(self.enums()))

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        tab_width = self.options.get("tab-width",
                                     self.state.document.settings.tab_width)
        lines = statemachine.string2lines("\n".join(template),
                                          tab_width,
                                          convert_whitespace=True)
        self.state_machine.insert_input(lines, source)
        return []
Пример #38
0
 def parse(self, inputstring, document):
     """Parse `inputstring` and populate `document`, a document tree."""
     self.setup_parse(inputstring, document)
     inputlines = statemachine.string2lines(inputstring,
                                            convert_whitespace=1)
     self.statemachine.run(inputlines, document, inliner=self.inliner)
     self.finish_parse()
Пример #39
0
 def run(self):
     result = super(ELSExp, self).run()
     if "nodoc" not in self.options:
         package = self.env.temp_data.get('el:package')
         node = addnodes.desc_content()
         string = DATA_DOC_STRINGS.get(package, {}) \
                                  .get(self.names[0][1], "")
         lines = string2lines(string)
         if lines and lines[-1].startswith('(fn '):
             lines = lines[:-1]
         self.state.nested_parse(StringList(lines), 0, node)
         if (result[1][1].children and
             isinstance(result[1][1][0], nodes.field_list)):
             cresult = result[1][1].deepcopy()
             target = result[1][1]
             target.clear()
             target.append(cresult[0])
             target.extend(node)
             target.extend(cresult[1:])
         else:
             cresult = result[1][1].deepcopy()
             target = result[1][1]
             target.clear()
             target.extend(node)
             target.extend(cresult)
     return result
Пример #40
0
 def test_parse(self):
     try:
         output = self.parser.parse(StringList(string2lines(self.input),
                                               'test data'))
     except Exception as details:
         output = '%s: %s' % (details.__class__.__name__, details)
     self.compare_output(self.input, pformat(output) + '\n',
                         pformat(self.expected) + '\n')
Пример #41
0
 def test_parse_table(self):
     self.parser.setup(StringList(string2lines(self.input), 'test data'))
     try:
         self.parser.find_head_body_sep()
         self.parser.parse_table()
         output = self.parser.cells
     except Exception, details:
         output = '%s: %s' % (details.__class__.__name__, details)
Пример #42
0
    def run(self):
        env = self.state.document.settings.env
        package = env.temp_data.get('el:package')
        keymap_list = DATA.get(package, {}).get('keymap', [])
        keymap_name = self.arguments[0]
        for keymap in keymap_list:
            if keymap['name'] == keymap_name:
                break
        else:
            return [self.state.reporter.warning(
                "Keymap {0} not found".format(keymap_name))]
        nodelist = []

        mapdoc = keymap['doc']
        if mapdoc:
            nd = nodes.paragraph()
            lines = string2lines(doc_to_rst(mapdoc))
            if lines and lines[-1].startswith('(fn '):
                lines = lines[:-1]
            self.state.nested_parse(StringList(lines), 0, nd)
            nodelist.append(nd)

        exclude = self.options.get('exclude', [])
        replace = self.options.get('replace', [])
        for keybind in filter_by_exclude_regexp_list(
                keymap['data'], exclude, lambda x: x['func']):
            desc = addnodes.desc()
            desc['domain'] = 'el'
            desc['objtype'] = 'keybind'
            desc['noindex'] = False
            signode = addnodes.desc_signature()
            # signode += addnodes.desc_annotation("", 'keybind ')
            key = simple_sed(replace, keybind['key'])
            signode += addnodes.desc_name("", key)
            signode += addnodes.desc_addname("", " " + keybind['func'])
            desc += signode
            if keybind['doc']:
                nd = addnodes.desc_content()
                lines = string2lines(doc_to_rst(keybind['doc']))
                if lines and lines[-1].startswith('(fn '):
                    lines = lines[:-1]
                self.state.nested_parse(StringList(lines), 0, nd)
                desc += nodes.definition("", nd)
            nodelist.append(desc)

        return nodelist
Пример #43
0
 def run (self):
     settings = self.state.document.settings
     include_lines = statemachine.string2lines (
         settings.get_resource ('mydocutils.gutenberg.parsers', self.resource).decode ('utf-8'), 
         settings.tab_width,
         convert_whitespace = 1)
     self.state_machine.insert_input (include_lines, '')
     return []
Пример #44
0
def _format_subcommand(command):
    """Format a sub-command of a `click.Command` or `click.Group`."""
    yield '.. object:: {}'.format(command.name)

    if command.short_help:
        yield ''
        for line in statemachine.string2lines(
                command.short_help, tab_width=4, convert_whitespace=True):
            yield _indent(line)
Пример #45
0
def _format_epilog(parser):
    """Get parser epilog.

    We parse this as reStructuredText, allowing users to embed rich
    information in their help messages if they so choose.
    """
    for line in statemachine.string2lines(
            parser.epilog, tab_width=4, convert_whitespace=True):
        yield line
Пример #46
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        if not self.state.document.settings.file_insertion_enabled:
            raise self.warning('"%s" directive disabled.' % self.name)
        source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))

        path = directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        encoding = self.options.get("encoding", self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get("tab-width", self.state.document.settings.tab_width)

        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe(
                'Problems with "%s" directive path:\n'
                'Cannot encode input file path "%s" '
                "(wrong locale?)." % (self.name, SafeString(path))
            )
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error)))
        startline = self.options.get("start-line", None)
        endline = self.options.get("end-line", None)
        try:
            if startline or (endline is not None):
                lines = include_file.readlines()
                rawtext = "".join(lines[startline:endline])
            else:
                rawtext = include_file.read()
        except UnicodeError as error:
            raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error)))

        include_lines = statemachine.string2lines(rawtext, tab_width, convert_whitespace=True)

        # default lexer to 'text'
        lexer = self.options.get("lexer", "text")

        self.options["source"] = path
        codeblock = Pygments(
            self.name,
            [lexer],  # arguments
            {},  # no options for this directive
            include_lines,  # content
            self.lineno,
            self.content_offset,
            self.block_text,
            self.state,
            self.state_machine,
        )
        return codeblock.run()
Пример #47
0
def _format_option(opt):
    """Format the output a `click.Option`."""
    is_multiple = opt.multiple
    opt = _get_help_record(opt)
    if opt is None:
        return
    yield '.. option:: {}'.format(opt[0])
    if opt[1]:
        yield ''
        lines = list(statemachine.string2lines(opt[1], tab_width=4, convert_whitespace=True))
        if is_multiple:
            lines[-1] += " (May specify more than once)"
        for line in lines:
            yield _indent(line)
    if opt[2]:
        yield ''
        for line in statemachine.string2lines(
                opt[2], tab_width=4, convert_whitespace=True):
            yield _indent(line)
Пример #48
0
def _format_option(opt):
    """Format the output for a `click.Option`."""
    opt = _get_help_record(opt)

    yield '.. option:: {}'.format(opt[0])
    if opt[1]:
        yield ''
        for line in statemachine.string2lines(
                opt[1], tab_width=4, convert_whitespace=True):
            yield _indent(line)
Пример #49
0
 def test_parse_table(self):
     self.parser.setup(StringList(string2lines(self.input), 'test data'))
     try:
         self.parser.find_head_body_sep()
         self.parser.parse_table()
         output = self.parser.cells
     except Exception as details:
         output = '%s: %s' % (details.__class__.__name__, details)
     self.compare_output(self.input, pformat(output) + '\n',
                         pformat(self.expected) + '\n')
Пример #50
0
 def run(self):
     service_name = self.arguments[0].strip()
     d = ServiceDescription(service_name)
     rawtext = self.generate_rst(d)
     tab_width = 4
     include_lines = statemachine.string2lines(
         rawtext, tab_width, convert_whitespace=1)
     self.state_machine.insert_input(
         include_lines, os.path.abspath(__file__))
     return []
Пример #51
0
 def render_cards(self, data, card_type):
     fett_templates = {
         "large": CARD_GROUP_TEMPLATE_LARGE,
         "small": CARD_GROUP_TEMPLATE_SMALL
     }
     if card_type in fett_templates.keys():
         rendered = fett_templates[card_type].render(data)
         rendered_lines = statemachine.string2lines(
             rendered, 4, convert_whitespace=1
         )
         self.state_machine.insert_input(rendered_lines, '')
Пример #52
0
 def run(self):
     content = self.get_content()
     try:
         docutils_input = io.StringInput(source=content)
         rawtext = docutils_input.read()
     except IOError as error:
         # Show the content
         raise self.severe(u'Problems with "%s" command:\n%s.' % ''.join(self.options['command']), ErrorString(error))
     include_lines = statemachine.string2lines(rawtext, 4, convert_whitespace=True)
     self.state_machine.insert_input(include_lines, 'CMD')
     return []
Пример #53
0
 def render(self, release_date, release_content):
     rendered_stitch_release_template = STITCH_RELEASE.render({
         "header": release_date,
         "slug_date": parse_date(release_date).isoformat().split("T")[0],
         "categories": [ReleaseCategory(category, items).asdict()
                        for category, items in release_content.items()],
     })
     rendered_rst_lines = statemachine.string2lines(
         rendered_stitch_release_template, 4, convert_whitespace=1
     )
     self.state_machine.insert_input(rendered_rst_lines, '')
Пример #54
0
    def add_rst(self, text):
        """
        Appends given reST into a managed list. It is NOT inserted into the
        document until commit_rst() is called

        Args:
          text (str): reST to track
        """
        if self.rst_lines is None:
            self.rst_lines = []

        self.rst_lines.extend(statemachine.string2lines(text))
Пример #55
0
    def read(self):  # type: ignore
        # type: () -> StringList
        inputstring = super().read()
        lines = string2lines(inputstring, convert_whitespace=True)
        content = StringList()
        for lineno, line in enumerate(lines):
            content.append(line, self.source_path, lineno)

        prepend_prolog(content, self.env.config.rst_prolog)
        append_epilog(content, self.env.config.rst_epilog)

        return content
Пример #56
0
 def run(self):
     # The section that should be documented
     if len(self.arguments):
         section = self.arguments[0]
     else:
         section = None
     link_targets = not ('nolinks' in self.options)
     rawtext = prefs.get_documentation(section, link_targets)
     include_lines = statemachine.string2lines(rawtext,
                                               convert_whitespace=True)
     self.state_machine.insert_input(include_lines, 'Brian preferences')
     return []
Пример #57
0
 def run(self):
     """
     Called by the Sphinx framework whenever the ..algorithm::
     directive is encountered
     """
     algname = str(self.arguments[0])
     rawtext = self._create_page_header(algname)
     tab_width = 4
     include_lines = statemachine.string2lines(rawtext, tab_width,
                                               convert_whitespace=True)
     self.state_machine.insert_input(include_lines, "")
     return []
Пример #58
0
        def run(self):
            try:
                rendered = HELP_TEMPLATE.render({
                    'role': icon_set.role_names[0],
                    'icons': sorted(icon_set.icons)
                })
            except Exception as error:
                raise self.severe('Failed to render template: {}'.format(ErrorString(error)))

            rendered_lines = statemachine.string2lines(rendered, 4, convert_whitespace=1)
            self.state_machine.insert_input(rendered_lines, '')
            return []
Пример #59
0
    def run(self):

        # Get object
        objname = self.arguments[0]
        try:
            __import__(objname)
            object = sys.modules[objname]
        except:
            self.warning("Cannot import object %s for overview" % objname)
            return []

        # Options
        config = self.state.document.settings.env.config
        # - titles
        titles = {}
        for title_name in "overview", "content":
            if self.options.has_key("title_" + title_name) and self.options["title_" + title_name] is not None:
                title = self.options["title_" + title_name]
            else:
                title = getattr(config, "overview_title_" + title_name)
            if not isinstance(title, (str, unicode)) or not title:
                title = False
            titles[title_name] = title
        # - underline
        if self.options.has_key("underline") and self.options["underline"] is not None:
            underline = self.options["underline"]
        else:
            underline = config.overview_underline
        underline = str(underline)[0]
        # - extra
        extra = {}
        for etype in "attributes", "functions", "classes", "methods", "class_attributes":
            etype = "extra_" + etype
            if self.options.has_key(etype) and self.options[etype] is not None:
                extra[etype] = self.options[etype]
        # - columns
        columns = self.options.get("columns", config.overview_columns)
        # - inheritance
        extra["inherited"] = "inherited-members" in self.options

        # Format
        raw_text = OverView(object, **extra).format(
            indent=0,
            title_overview=titles["overview"],
            title_content=titles["content"],
            underline=underline,
            columns=columns,
        )
        source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
        include_lines = string2lines(raw_text, convert_whitespace=1)
        self.state_machine.insert_input(include_lines, source)

        return []