Beispiel #1
0
     def read(self):
         if self.source_path.endswith(".rst"):
             return FileInput.read(self)

         data = FileInput.read(self).split("\n")
         newdata = []

         line = data.pop(0)
         while data:
             if len(line.strip()) == 0:
                 newdata.append('')
                 line = data.pop(0)

             elif line.startswith("#"):
                 while data and line.startswith('#'):
                     newdata.append(line[2:])
                     line = data.pop(0)

             else:
                 newdata.extend(['', '::', ''])

                 while data and not line.startswith('#'):
                     newdata.append('    ' + line)
                     line = data.pop(0)

                 newdata.append('')

         return "\n".join(newdata)
Beispiel #2
0
def run(args):
    """The main function."""

    sources = []
    if args.debug:
        from mjstat.testdata import TEST_INPUT
        sources.append(StringInput(source=TEST_INPUT))
    else:
        # XXX
        if isinstance(args.input, (list, tuple)):
            sources.extend(
                FileInput(source_path=i, encoding='sjis', mode='r')
                for i in args.input)
        else:
            sources.append(
                FileInput(source_path=args.input, encoding='sjis', mode='r'))

    parser = MJScoreParser()
    reader = MJScoreReader()

    game_data_list = tuple(reader.read(i, parser, args) for i in sources)
    game_data = merge_games(game_data_list)
    apply_transforms(game_data)

    writer = MJScoreWriter()
    writer.write(game_data, FileOutput(None))
Beispiel #3
0
    def __init__(self, app, env, *args, **kwds):
        # type: (Sphinx, BuildEnvironment, Any, Any) -> None
        self.app = app
        self.env = env

        # set up error handler
        codecs.register_error('sphinx', self.warn_and_replace)  # type: ignore

        kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
        FileInput.__init__(self, *args, **kwds)
Beispiel #4
0
    def __init__(self, app, env, *args, **kwds):
        # type: (Sphinx, BuildEnvironment, Any, Any) -> None
        self.app = app
        self.env = env

        # set up error handler
        codecs.register_error('sphinx', self.warn_and_replace)  # type: ignore

        kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
        FileInput.__init__(self, *args, **kwds)
Beispiel #5
0
    def patch(self, out_filename=None):
        """Apply patches and write changes to specified file.

        Args:
            out_filename: Output filename. If not set then input one is used.
        """
        in_ = FileInput(source_path=self._filename, encoding=self._encoding)
        content = in_.read()
        content = self._patcher.patch(content)
        content = '\n'.join(content)

        out = FileOutput(destination_path=out_filename or self._filename,
                         encoding=self._encoding)
        out.write(content)
Beispiel #6
0
    def read(self):
        # type: () -> unicode
        def get_parser_type(source_path):
            # type: (unicode) -> Tuple[unicode]
            for suffix in self.env.config.source_parsers:
                if source_path.endswith(suffix):
                    parser_class = self.env.config.source_parsers[suffix]
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(
                            parser_class,
                            'source parser')  # type: ignore  # NOQA
                    return parser_class.supported
            else:
                return ('restructuredtext', )

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
Beispiel #7
0
def imap_directive(name, arguments, options, content, lineno,
                   content_offset, block_text, state, state_machine):
    """Render an image <map> element."""

    encoding = options.get('encoding', state.document.settings.input_encoding)

    image_alt = options.get('alt', '')
    image_class = options.get('class', '')

    attributes['source'] = imap_source = arguments[0]

    source_dir = os.path.dirname(
        os.path.abspath(state.document.current_source)
        )

    path = os.path.normpath(os.path.join(source_dir, imap_source))
    path = relative_path(None, path)

    try:
        state.document.settings.record_dependencies.add(path)
        raw_file = FileInput(
            source_path=path, encoding=encoding,
            error_handler=state.document.settings.input_encoding_error_handler,
            handle_io_errors=None)
    except IOError, error:
        severe = state_machine.reporter.severe(
            'Problems with "%s" directive path:\n%s.' % (name, error),
            nodes.literal_block(block_text, block_text), line=lineno)
        return [severe]
Beispiel #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)

        obj_list = self.arguments[0].split()

        # Concatenate the backreferences file(s)
        lines = []
        for obj in obj_list:
            path = os.path.join(os.getcwd(), 'generated', 'modules',
                                f'{obj}.examples')
            lines += (FileInput(
                source_path=path).readlines())[5:]  # slice removes heading

        # Append the end for the gallery
        lines += [
            '\n', '.. raw:: html\n', '\n',
            '    <div class="sphx-glr-clear"></div>\n'
        ]
        text = ''.join(lines)

        include_lines = statemachine.string2lines(text,
                                                  convert_whitespace=True)
        self.state_machine.insert_input(include_lines, path)

        return []
Beispiel #9
0
    def run(self):
        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))
        file_path = rst.directives.path(self.arguments[0])
        absolute_path = os.path.normpath(os.path.join(source_dir, file_path))
        path = relative_path(None, absolute_path)

        settings = self.state.document.settings

        try:
            settings.record_dependencies.add(path)
            include_file = FileInput(
                source_path=path,
                encoding=self.options.get("encoding", settings.input_encoding),
                error_handler=settings.input_encoding_error_handler,
            )
        except UnicodeEncodeError as error:
            raise self.severe(f'Problems with "{self.name}" directive path:\n'
                              f'Cannot encode input file path "{path}" '
                              "(wrong locale?).")
        except IOError as error:
            raise self.severe(
                f'Problems with "{self.name}" directive path:\n{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(
                f'Problem with "{self.name}" directive:\n{ErrorString(error)}')

        document = new_document(absolute_path, settings)
        MystParser().parse(rawtext, document)

        return document.children
Beispiel #10
0
 def __set__(self, instance, value):
     if isinstance(value, str):
         if os.path.isfile(value):
             source = FileInput(source_path=os.path.abspath(value))
         else:
             source = StringInput(value)
     else:
         source = NullInput()
     super().__set__(instance, source)
Beispiel #11
0
 def read(self):
     data = FileInput.read(self)
     if self.app:
         arg = [data]
         self.app.emit('source-read', self.env.docname, arg)
         data = arg[0]
     if self.env.config.rst_epilog:
         data = data + '\n' + self.env.config.rst_epilog + '\n'
     if self.env.config.rst_prolog:
         data = self.env.config.rst_prolog + '\n' + data
     return data
Beispiel #12
0
    def read(self):
        # type: () -> unicode
        """Reads the contents from file.

        After reading, it emits Sphinx event ``source-read``.
        """
        data = FileInput.read(self)

        # emit source-read event
        arg = [data]
        self.app.emit('source-read', self.env.docname, arg)
        return arg[0]
Beispiel #13
0
 def read(self):
     data = FileInput.read(self)
     if self.app:
         arg = [data]
         self.app.emit('source-read', self.env.docname, arg)
         data = arg[0]
     docinfo, data = split_docinfo(data)
     if self.env.config.rst_epilog:
         data = data + '\n' + self.env.config.rst_epilog + '\n'
     if self.env.config.rst_prolog:
         data = self.env.config.rst_prolog + '\n' + data
     return docinfo + data
Beispiel #14
0
    def read(self):
        # type: () -> unicode
        """Reads the contents from file.

        After reading, it emits Sphinx event ``source-read``.
        """
        data = FileInput.read(self)

        # emit source-read event
        arg = [data]
        self.app.emit('source-read', self.env.docname, arg)
        return arg[0]
Beispiel #15
0
    def read(self):
        # type: () -> unicode
        def get_parser_type(source_path):
            # type: (unicode) -> Tuple[unicode]
            for suffix, parser_class in iteritems(self.app.registry.get_source_parsers()):
                if source_path.endswith(suffix):
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(parser_class, 'source parser')  # type: ignore  # NOQA
                    return parser_class.supported
            return ('restructuredtext',)

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
Beispiel #16
0
 def run(self):
     if not self.state.document.settings.file_insertion_enabled:
         raise self.warning('"%s" directive disabled.' % self.name)
     path = directives.path(self.arguments[0])
     # ALL the included files are relative to the repository root.
     # we need to remove absolute paths
     if path.startswith('/'):
         raise self.severe('Problem with "%s" directive path:\npath ' \
                           'should be relative' % self.name)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     tab_width = self.options.get('tab-width',
                                  self.state.document.settings.tab_width)
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = FileInput(
             source=BlohgFile(path), encoding=encoding,
             error_handler=(self.state.document.settings.\
                            input_encoding_error_handler),
             handle_io_errors=None)
     except IOError, error:
         raise self.severe(u'Problems with "%s" directive path:\n%s.' %
                           (self.name, ErrorString(error)))
Beispiel #17
0
    def read(self):
        def get_parser_type(source_path):
            for suffix in self.env.config.source_parsers:
                if source_path.endswith(suffix):
                    parser_class = self.env.config.source_parsers[suffix]
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(parser_class, 'source parser')
                    return parser_class.supported
            else:
                return ('restructuredtext',)

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
Beispiel #18
0
 def run(self):
     if not self.state.document.settings.file_insertion_enabled:
         raise self.warning('"%s" directive disabled.' % self.name)
     path = directives.path(self.arguments[0])
     # ALL the included files are relative to the repository root.
     # we need to remove absolute paths
     if path.startswith('/'):
         raise self.severe('Problem with "%s" directive path:\npath ' \
                           'should be relative' % self.name)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     tab_width = self.options.get('tab-width',
                                  self.state.document.settings.tab_width)
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = FileInput(
             source=BlohgFile(path), encoding=encoding,
             error_handler=(self.state.document.settings.\
                            input_encoding_error_handler))
     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)))
     # start-after/end-before: no restrictions on newlines in match-text,
     # and no restrictions on matching inside lines vs. line boundaries
     after_text = self.options.get('start-after', None)
     if after_text:
         # skip content in rawtext before *and incl.* a matching text
         after_index = rawtext.find(after_text)
         if after_index < 0:
             raise self.severe('Problem with "start-after" option of "%s" '
                               'directive:\nText not found.' % self.name)
         rawtext = rawtext[after_index + len(after_text):]
     before_text = self.options.get('end-before', None)
     if before_text:
         # skip content in rawtext after *and incl.* a matching text
         before_index = rawtext.find(before_text)
         if before_index < 0:
             raise self.severe('Problem with "end-before" option of "%s" '
                               'directive:\nText not found.' % self.name)
         rawtext = rawtext[:before_index]
     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(rawtext,
                                                   tab_width,
                                                   convert_whitespace=1)
         self.state_machine.insert_input(include_lines, path)
         return []
Beispiel #19
0
 def __init__(self, app, env, *args, **kwds):
     # type: (Sphinx, BuildEnvironment, Any, Any) -> None
     self.app = app
     self.env = env
     kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
     FileInput.__init__(self, *args, **kwds)
Beispiel #20
0
 def __init__(self, app, env, *args, **kwds):
     self.app = app
     self.env = env
     kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
     FileInput.__init__(self, *args, **kwds)
Beispiel #21
0
 def __init__(self, app, env, *args, **kwds):
     # type: (Sphinx, BuildEnvironment, Any, Any) -> None
     self.app = app
     self.env = env
     kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
     FileInput.__init__(self, *args, **kwds)
Beispiel #22
0
 def __init__(self, app, env, *args, **kwds):
     self.app = app
     self.env = env
     kwds['error_handler'] = 'sphinx'  # py3: handle error on open.
     FileInput.__init__(self, *args, **kwds)
Beispiel #23
0
def onIconDoubleClick(tag, keywords):

    c = keywords.get("c")
    p = keywords.get("p")
    # g.trace(c)

    if not c or not p:
        return

    applyConfiguration(c)
    config.tag = tag

    h = p.headString().strip()

    if g.match_word(h, 0, "@rst"):
        if len(h) > 5:
            fname = h[5:]
            ext = os.path.splitext(fname)[1].lower()
            if ext in ('.htm', '.html', '.tex'):
                #@                << write rST as HTML/LaTeX >>
                #@+node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >>
                try:
                    import docutils
                except:
                    g.es('HTML/LaTeX generation requires docutils')
                    return
                else:
                    import docutils.parsers.rst
                    from docutils.core import Publisher
                    from docutils.io import StringOutput, StringInput, FileOutput, FileInput
                    import StringIO

                # Set the writer and encoding for the converted file
                if ext in ('.html', '.htm'):
                    writer = 'html'
                    enc = "utf-8"
                else:
                    writer = 'latex'
                    enc = "iso-8859-1"

                syntax = False
                if writer == 'html':
                    try:
                        import SilverCity

                        #@        << define code-block >>
                        #@+node:ekr.20040331071319.5:<< define code-block >>
                        def code_block(name, arguments, options, content,
                                       lineno, content_offset, block_text,
                                       state, state_machine):
                            """Create a code-block directive for docutils."""

                            # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170
                            language = arguments[0]
                            module = getattr(SilverCity, language)
                            generator = getattr(module,
                                                language + "HTMLGenerator")
                            io = StringIO.StringIO()
                            generator().generate_html(io, '\n'.join(content))
                            html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue(
                            )
                            raw = docutils.nodes.raw(
                                '', html, format='html'
                            )  #(self, rawsource='', text='', *children, **attributes):
                            return [raw]

                        # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html.
                        code_block.arguments = (
                            1,  # Number of required arguments.
                            0,  # Number of optional arguments.
                            0
                        )  # True if final argument may contain whitespace.

                        # A mapping from option name to conversion function.
                        code_block.options = {
                            'language': docutils.parsers.rst.directives.
                            unchanged  # Return the text argument, unchanged
                        }

                        code_block.content = 1  # True if content is allowed.

                        # Register the directive with docutils.
                        docutils.parsers.rst.directives.register_directive(
                            'code-block', code_block)

                        config.do_replace_code_blocks = False
                        #@nonl
                        #@-node:ekr.20040331071319.5:<< define code-block >>
                        #@nl
                        syntax = True
                    except ImportError:
                        g.es(
                            'SilverCity not present so no syntax highlighting')
                        #@        << define alternate code block implementation>>
                        #@+node:bwmulder.20050326114320: << define alternate code block implementation>>
                        # Don't know what to do here: Can someone make a suggestion?
                        #import docutils.parsers.rst.directives.admonitions
                        #import docutils.parsers.rst.directives.body
                        # docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.block
                        # docutils.parsers.rst.directives.register_directive('code-block', docutils.parsers.rst.directives.admonitions.admonition)
                        #docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.pull_quote
                        # g.es("Registered some alternate implementation for code-block directive")
                        config.do_replace_code_blocks = config.rst2_replace_code_blocks
                        #@-node:bwmulder.20050326114320: << define alternate code block implementation>>
                        #@nl

                if config.rst2file:
                    rstFileName = os.path.splitext(fname)[0] + ".txt"
                    rstFile = file(rstFileName, "w")
                    g.es("Using %s as rst file" % rstFileName)
                else:
                    rstFile = StringIO.StringIO()
                config.current_file = fname
                writeTreeAsRst(rstFile, fname, p, c, syntax=syntax)
                if config.rst2file:
                    rstFile.close()
                else:
                    rstText = rstFile.getvalue()

                # This code snipped has been taken from code contributed by Paul Paterson 2002-12-05.
                pub = Publisher()
                if config.rst2file:
                    pub.source = FileInput(source_path=rstFileName)
                    pub.destination = FileOutput(destination_path=fname,
                                                 encoding='unicode')
                else:
                    pub.source = StringInput(source=rstText)
                    pub.destination = StringOutput(pub.settings, encoding=enc)
                pub.set_reader('standalone', None, 'restructuredtext')
                pub.set_writer(writer)
                output = pub.publish(argv=[''])

                if config.rst2file:
                    pass
                else:
                    convertedFile = file(fname, 'w')
                    convertedFile.write(output)
                    convertedFile.close()
                rstFile.close()
                writeFullFileName(fname)
                return http_support_main(tag, fname)

                #@-node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >>
                #@nl
            else:
                #@                << write rST file >>
                #@+node:ekr.20040331071319.6:<< write rST file >>
                rstFile = file(fname, 'w')
                writeTreeAsRst(rstFile, fname, p, c)
                rstFile.close()
                writeFullFileName(fname)
                #@nonl
                #@-node:ekr.20040331071319.6:<< write rST file >>
                #@nl
        else:
            # if the headline only contains @rst then open the node and its parent in text editor
            # this works for me but needs to be generalized and should probably be a component
            # of the open_with plugin.
            if 0:
                c.openWith(("os.startfile", None, ".txt"))
                c.selectVnode(p.parent())
                c.openWith(("os.startfile", None, ".tp"))