Ejemplo n.º 1
0
def _get_lexer(codesyntax):
    if codesyntax in ("cpp", "javascript"):
        return lexers.JavascriptLexer()
    elif codesyntax == "python":
        return lexers.PythonLexer()
    elif codesyntax == "json":
        return lexers.JsonLexer()
    elif codesyntax == "xml" or codesyntax == "html":
        return lexers.HtmlLexer()
    elif codesyntax == "yml" or codesyntax == "yaml":
        return lexers.YamlLexer()
    elif codesyntax == "css":
        return lexers.CssLexer()
    elif codesyntax == "sql":
        return lexers.SqlLexer()
    elif codesyntax == "bash" or codesyntax == "sh":
        return lexers.BashLexer()
    elif codesyntax == "go":
        return lexers.GoLexer()
    elif codesyntax == "diff":
        return lexers.DiffLexer()
    elif codesyntax == "emacslisp":
        return lexers.EmacsLispLexer()
    elif codesyntax == "lisp":
        return lexers.CommonLispLexer()
    elif codesyntax == "rust":
        return lexers.RustLexer()
    elif codesyntax == "jsx":
        return BabylonLexer()
    elif codesyntax:
        raise NotImplementedError(codesyntax)
    else:
        return lexers.TextLexer()
Ejemplo n.º 2
0
    def handle(self, *args, **options):

        try:
            from pygments import lexers
        except ImportError:
            self.stdout.write("This command requires the Pygments package.")
            self.stdout.write("Please install it with:\n\n")
            self.stdout.write("  pip install Pygments\n\n")
            return

        # Invocation examples
        _process("bash_curl", lexers.BashLexer())
        _process("bash_wget", lexers.BashLexer())
        _process("browser", lexers.JavascriptLexer())
        _process("crontab", lexers.BashLexer())
        _process("cs", lexers.CSharpLexer())
        _process("node", lexers.JavascriptLexer())
        _process("go", lexers.GoLexer())
        _process("python_urllib2", lexers.PythonLexer())
        _process("python_requests", lexers.PythonLexer())
        _process("python_requests_fail", lexers.PythonLexer())
        _process("python_requests_start", lexers.PythonLexer())
        _process("python_requests_payload", lexers.PythonLexer())
        _process("php", lexers.PhpLexer())
        _process("powershell", lexers.shell.PowerShellLexer())
        _process("powershell_inline", lexers.shell.BashLexer())
        _process("ruby", lexers.RubyLexer())
Ejemplo n.º 3
0
def run(out):
    snippets = []

    py_lexer = lexers.PythonLexer()
    go_lexer = lexers.GoLexer()
    html_formatter = HtmlFormatter()

    for snippet_dir in _dirs():
        go_codes = []
        for f in glob(os.path.join(snippet_dir, '*.go')):
            go_codes.append((f, codecs.open(f, 'r', 'utf8').read()))
        if not go_codes:
            print snippet_dir, "has no .go code"
            continue

        py_codes = []
        for f in glob(os.path.join(snippet_dir, '*.py')):
            py_codes.append((f, codecs.open(f, 'r', 'utf8').read()))
        if not py_codes:
            print snippet_dir, "has no .py code"
            continue

        id = os.path.basename(snippet_dir)
        if os.path.isfile(os.path.join(snippet_dir, 'title')):
            title = open(os.path.join(snippet_dir, 'title')).read().strip()
        else:
            title = id.replace('_', ' ').title()

        readme = None
        for f in glob(os.path.join(snippet_dir, '*.md')):
            if readme is not None:
                raise SnippetError('%s contains multiple .md files')
            with codecs.open(f, 'r', 'utf-8') as reader:
                readme = special_markdown(reader.read())
        snippets.append({
            'id':
            id,
            'title':
            title,
            'readme':
            readme,
            'go_codes':
            [(filename, pygments.highlight(code, go_lexer, html_formatter))
             for filename, code in go_codes],
            'py_codes':
            [(filename, pygments.highlight(code, py_lexer, html_formatter))
             for filename, code in py_codes],
        })

    template = env.get_template('build.html')
    html = template.render(
        snippets=snippets,
        highlight_css=html_formatter.get_style_defs('.highlight'),
        bootstrap_css=env.get_template('bootstrap.min.css').render())
    if out:
        codecs.open(out, 'w', 'utf-8').write(html)
    else:
        print html
Ejemplo n.º 4
0
 def _get_lexer(codesyntax):
     if codesyntax == 'python':
         return lexers.PythonLexer()
     elif codesyntax == 'go':
         return lexers.GoLexer()
     elif codesyntax:
         raise NotImplementedError(codesyntax)
     else:
         return lexers.TextLexer()
Ejemplo n.º 5
0
def run(out, dry_run=False, check_built=False):
    snippets = []

    py_lexer = lexers.PythonLexer()
    go_lexer = lexers.GoLexer()
    html_formatter = HtmlFormatter()

    for snippet_dir in _dirs():
        go_codes = []
        for fn in glob(os.path.join(snippet_dir, "*.go")):
            with open(fn) as f:
                go_codes.append((fn, f.read()))
        if not go_codes:
            print(snippet_dir, "has no .go code")
            continue

        py_codes = []
        for fn in glob(os.path.join(snippet_dir, "*.py")):
            with open(fn) as f:
                py_codes.append((fn, f.read()))
        if not py_codes:
            print(snippet_dir, "has no .py code")
            continue

        id = os.path.basename(snippet_dir)
        if os.path.isfile(os.path.join(snippet_dir, "title")):
            with open(os.path.join(snippet_dir, "title")) as f:
                title = f.read().strip()
        else:
            title = id.replace("_", " ").title()

        readme = None
        for fn in glob(os.path.join(snippet_dir, "*.md")):
            if readme is not None:
                raise SnippetError("%s contains multiple .md files")
            with open(fn) as f:
                readme = special_markdown(f.read())
        snippets.append(
            {
                "id": id,
                "title": title,
                "readme": readme,
                "go_codes": [
                    (filename, pygments.highlight(code, go_lexer, html_formatter))
                    for filename, code in go_codes
                ],
                "py_codes": [
                    (filename, pygments.highlight(code, py_lexer, html_formatter))
                    for filename, code in py_codes
                ],
            }
        )

    template = env.get_template("build.html")
    html = template.render(
        snippets=snippets,
        highlight_css=html_formatter.get_style_defs(".highlight"),
        bootstrap_css=env.get_template("bootstrap.min.css").render(),
    )
    if dry_run:
        # Everything worked!
        return 0
    if check_built:
        # Raise an error if the newly created HTML isn't the same as what
        # was created before.
        with open(out) as f:
            before = f.read()
        lines_before = [x.strip() for x in before.strip().splitlines() if x.strip()]
        lines_html = [x.strip() for x in html.strip().splitlines() if x.strip()]
        if lines_before != lines_html:
            import difflib

            print(
                "".join(
                    difflib.unified_diff(
                        before.splitlines(True),
                        html.splitlines(True),
                        fromfile="HTML before",
                        tofile="New HTML",
                    )
                )
            )
            raise CheckBuiltFailed(
                "The generated HTML is different from what it was before. "
                "That means that the HTML made from the snippets doesn't match "
                "the build HTML. Run this script without --check-built and check "
                "in the changes to the dist HTML."
            )
    elif out:
        with open(out, "w") as f:
            f.write(html)
            f.write("\n")
    else:
        print(html)