Exemple #1
0
 def _format(self, format_name, caller=None):
     body = caller()
     text = format_text(self.environment.app,
                        format_name,
                        Markup(body.rstrip()).unescape(),
                        exact_format=True)
     return text
Exemple #2
0
    def _highlight(self, lang, parameters, caller=None):
        # highlight code using Pygments
        body = caller()
        try:
            if lang is None:
                lexer = guess_lexer(body)
            else:
                lexer = get_lexer_by_name(lang, stripall=False)
        except ClassNotFound as e:
            if lang == 'dataspec':
                lexer = DataSpecLexer(stripall=False)
            else:
                print(e)
                sys.exit(1)

        if ctags:
            if 'tagsfile' not in parameters:
                parameters['tagsfile'] = module_path() + '/pages/site/spectags'

            if 'tagurlformat' not in parameters:
                lang = 'en'
                if hasattr(g, 'lang') and g.lang:
                    lang = g.lang
                parameters['tagurlformat'] = '/' + lang + '/%(path)s%(fname)s'

        formatter = I2PHtmlFormatter(**parameters)
        code = highlight(Markup(body).unescape(), lexer, formatter)
        return code
Exemple #3
0
    def _highlight(self,
                   lang,
                   line_numbers=False,
                   use_classes=False,
                   css_class=None,
                   css_id=None,
                   caller=None):
        # Try to be mostly compatible with Jinja2-highlight's settings.
        body = caller()

        if lang is None:
            lexer = guess_lexer(body)
        else:
            lexer = get_lexer_by_name(lang, stripall=False)

        if css_class is None:
            try:
                css_class = self.environment.jinja2_highlight_cssclass
            except AttributeError:
                pass

        if css_class is not None:
            formatter = HtmlFormatter(cssclass=css_class, linenos=line_numbers)
        else:
            formatter = HtmlFormatter(linenos=line_numbers)

        code = highlight(Markup(body.rstrip()).unescape(), lexer, formatter)
        return code
Exemple #4
0
def highlight_and_link_json(content):
    """
    Given JSON text, syntax-highlight it and convert URLs to links.
    """
    html = highlight(content, LEXER, FORMATTER)
    urlized_html = linker(html)
    return Markup(urlized_html)
Exemple #5
0
def codefile(lang, path):
    lexer = get_lexer_by_name(lang, stripall=False)
    formatter = HtmlFormatter()

    with (here / path).open() as f:
        body = f.read()

    return highlight(Markup(body).unescape(), lexer, formatter)
Exemple #6
0
def show_blob(reponame, branchname, path):
    '''
    Shows a given blob for the git repo
    '''
    repopath = os.path.join(PATH, reponame)
    if not os.path.exists(repopath):
        return "Sorry"
    data = get_blob_text(repopath, path, branchname)
    return render_template('blob.html', text=Markup(data).unescape())
Exemple #7
0
def highlight_and_link_json(content):
    """
    Given JSON text, syntax-highlight it and convert URLs to links.
    """
    formatter = HtmlFormatter()
    lexer = get_lexer_by_name('json')
    html = highlight(content, lexer, formatter)
    urlized_html = linker(html)
    return Markup(urlized_html)
Exemple #8
0
def code(body, lang, sample=None):
    lexer = get_lexer_by_name(lang, stripall=False)
    formatter = HtmlFormatter()
    body = textwrap.dedent(body.rstrip())

    sample = getattr(samples, sample, None)
    if sample:
        result = sample(body.strip())
    else:
        result = None

    html = highlight(Markup(body).unescape(), lexer, formatter)

    if result:
        formatter = HtmlFormatter(cssclass='output')
        html += highlight(Markup(result).unescape(), lexer, formatter)

    return html
Exemple #9
0
    def _format_currency(self, amount, currency, currency_last, separator,
                         **kwargs):
        if kwargs:
            amount = """<span %s data-sep="%s">%s</span>""" % (" ".join(
                '%s="%s"' % (key, value)
                for key, value in kwargs.iteritems()), separator, amount)

        params = ((amount, currency) if currency_last else (currency, amount))

        return Markup("%s%s" % params)
Exemple #10
0
def describe_term_language(lang, description_language='en'):
    if description_language != 'en':
        raise NotImplementedError(
            "We don't support non-English interface text yet.")

    language_name = get_language_name(lang, description_language)
    if language_name[0] in 'AEIOU' and not language_name.startswith('Uk'):
        article = 'An'
    else:
        article = 'A'

    content = '{article} <a href="/c/{lang}">{language_name}</a> term'.format(
        article=article, lang=lang, language_name=language_name)
    return Markup(content)
Exemple #11
0
def get_blob_text(repopath, path, branchname='master'):
    repo = Repo(repopath)
    git = repo.git
    text = None
    text = git.show('%s:%s' % (branchname, path))
    mime_type = get_mime_type(repo, branchname, path)
    try:
        lexer = get_lexer_for_mimetype(mime_type)
    except ClassNotFound:
        lexer = get_lexer_by_name('text')
    formatter = HtmlFormatter(linenos=True,
                              lineanchors='line',
                              anchorlinenos=True)
    result = highlight(Markup(text).unescape(), lexer, formatter)
    return result
Exemple #12
0
    def _highlight(self, lang, caller=None):
        # highlight code using Pygments
        body = caller()
        try:
            if lang is None:
                lexer = guess_lexer(body)
            else:
                lexer = get_lexer_by_name(lang, stripall=False)
        except ClassNotFound as e:
            print(e)
            sys.exit(1)

        formatter = HtmlFormatter()
        code = highlight(Markup(body).unescape(), lexer, formatter)
        return code
Exemple #13
0
def describe_term_language(lang):
    """
    Take in a language code for a ConceptNet term, and output an English
    phrase describing its language, such as 'A French term' or 'An English
    term'.
    """
    language_name = get_language_name(lang)
    if language_name[0] in 'AEIOU' and not language_name.startswith('Uk'):
        article = 'An'
    else:
        article = 'A'

    content = '{article} <a href="/c/{lang}">{language_name}</a> term'.format(
        article=article, lang=lang, language_name=language_name)
    return Markup(content)
Exemple #14
0
    def _highlight(self, lang, linenos, caller=None):
        # highlight code using Pygments
        body = caller()

        # Check the globals to see if a cssclass setting for Pygment's HtmlFormatter
        # has been set
        try:
            cssclass = self.environment.jinja2_highlight_cssclass
        except AttributeError:
            cssclass = None

        try:
            if lang is None:
                lexer = guess_lexer(body)
            else:
                lexer = get_lexer_by_name(lang, stripall=False)
        except ClassNotFound as e:
            print(e)
            sys.exit(1)

        # Set the cssclass if we have one
        # The linenos setting expects either 'inline' or 'table', as per Pygment's
        # settings, any true value except 'inline' results in 'table'. If linenos
        # hasn't been assigned in parse it will be None and will result in no
        # line numbers
        if cssclass is not None:
            formatter = HtmlFormatter(cssclass=cssclass, linenos=linenos)
        else:
            formatter = HtmlFormatter(linenos=linenos)

        # If you place the tag on the line under the code, like this;
        # pint_glass.drink()
        # {% endhighlight %}
        # The result will have an extra blank line underneath, this can cause an extra
        # blank line of line numbering.
        # Use rstrip to remove extraneous white space
        code = highlight(Markup(body.rstrip()).unescape(), lexer, formatter)
        return code
Exemple #15
0
 def _highlight(self, lang, caller=None):
     body = caller()
     lexer = get_lexer_by_name(lang, stripall=False)
     formatter = HtmlFormatter(nowrap=True, style=self.style)
     code = highlight(Markup(body.rstrip()).unescape(), lexer, formatter)
     return code
Exemple #16
0
def describe_sources(sources, specific=True):
    """
    Build a marked-up text phrase describing the sources of our data.

    If `specific` is True, sources with many known individual contributors
    will list up to MAX_INDIVIDUALS of those contributors. If False, only
    the source as a whole will be credited. specific=False is used for the
    credit at the top of a page.
    """
    omcs_contributors = []
    omcs_count = 0
    ptt_count = 0
    nadya_count = 0
    more_sources = set()

    for source in sources:
        if 'activity' in source and source[
                'activity'] == '/s/activity/omcs/nadya.jp':
            nadya_count += 1
        elif 'activity' in source and source[
                'activity'] == '/s/activity/kyoto_yahoo':
            more_sources.add(
                source_link(source['activity'], KYOTO_YAHOO_CREDIT))
        elif 'contributor' in source:
            contributor = source['contributor']
            prefix = uri_prefix(contributor, 3)
            if prefix == '/s/contributor/omcs':
                if len(omcs_contributors) < MAX_INDIVIDUALS:
                    name = split_uri(contributor)[-1]
                    omcs_contributors.append(source_link(contributor, name))
                omcs_count += 1
            elif prefix == '/s/contributor/petgame':
                ptt_count += 1
            elif contributor in CONTRIBUTOR_NAME_MAP:
                more_sources.add(
                    source_link(contributor,
                                CONTRIBUTOR_NAME_MAP[contributor]))
            else:
                more_sources.add(source_link(contributor, contributor))

    source_chunks = []
    if omcs_contributors:
        if specific:
            if omcs_count > MAX_INDIVIDUALS:
                omcs_contributors.append("{} more".format(omcs_count -
                                                          MAX_INDIVIDUALS))

            omcs_str = '<a href="/s/activity/omcs">Open Mind Common Sense</a> contributors {}'.format(
                oxford_comma(omcs_contributors))
            source_chunks.append(omcs_str)
        else:
            source_chunks.append(
                '<a href="/s/activity/omcs">Open Mind Common Sense</a> contributors'
            )
    if ptt_count:
        if specific:
            if ptt_count == 1:
                count_str = "a player"
            else:
                count_str = "{} players".format(ptt_count)
            source_chunks.append(
                '{} of the <a href="/s/contributor/petgame">PTT Pet Game</a>'.
                format(count_str))
        else:
            source_chunks.append(
                'the <a href="/s/contributor/petgame">PTT Pet Game</a>')

    if nadya_count:
        if specific:
            if nadya_count == 1:
                count_str = "a player"
            else:
                count_str = "{} players".format(nadya_count)
            source_chunks.append(
                '{} of <a href="/s/activity/omcs/nadya.jp">nadya.jp</a>'.
                format(count_str))
        else:
            source_chunks.append(
                '<a href="/s/activity/omcs/nadya.jp">nadya.jp</a>')

    source_chunks.extend(sorted(more_sources))
    if len(source_chunks) == 1:
        source_markup = "<strong>Source:</strong> {}".format(source_chunks[0])
    else:
        source_markup = "<strong>Sources:</strong> {}".format(
            oxford_comma(source_chunks))
    return Markup(source_markup)
Exemple #17
0
def describe_sources(sources, specific=True):
    omcs_contributors = []
    omcs_count = 0
    ptt_count = 0
    nadya_count = 0
    more_sources = set()

    for source in sources:
        if 'activity' in source and source[
                'activity'] == '/s/activity/omcs/nadya.jp':
            nadya_count += 1
        elif 'contributor' in source:
            contributor = source['contributor']
            prefix = uri_prefix(contributor, 3)
            if prefix == '/s/contributor/omcs':
                if len(omcs_contributors) < MAX_INDIVIDUALS:
                    name = split_uri(contributor)[-1]
                    omcs_contributors.append(source_link(contributor, name))
                omcs_count += 1
            elif prefix == '/s/contributor/petgame':
                ptt_count += 1
            elif prefix == '/s/resource/en.wiktionary.org':
                more_sources.add(source_link(prefix, "English Wiktionary"))
            elif prefix == '/s/resource/de.wiktionary.org':
                more_sources.add(source_link(prefix, "German Wiktionary"))
            elif prefix == '/s/resource/fr.wiktionary.org':
                more_sources.add(source_link(prefix, "French Wiktionary"))
            elif contributor in CONTRIBUTOR_NAME_MAP:
                more_sources.add(
                    source_link(contributor,
                                CONTRIBUTOR_NAME_MAP[contributor]))
            else:
                more_sources.add(source_link(contributor, contributor))

    source_chunks = []
    if omcs_contributors:
        if specific:
            if omcs_count > MAX_INDIVIDUALS:
                omcs_contributors.append("{} more".format(omcs_count -
                                                          MAX_INDIVIDUALS))

            omcs_str = '<a href="/s/activity/omcs">Open Mind Common Sense</a> contributors {}'.format(
                oxford_comma(omcs_contributors))
            source_chunks.append(omcs_str)
        else:
            source_chunks.append(
                '<a href="/s/activity/omcs">Open Mind Common Sense</a> contributors'
            )
    if ptt_count:
        if specific:
            if ptt_count == 1:
                count_str = "a player"
            else:
                count_str = "{} players".format(ptt_count)
            source_chunks.append(
                '{} of the <a href="/s/contributor/petgame">PTT Pet Game</a>'.
                format(count_str))
        else:
            source_chunks.append(
                'the <a href="/s/contributor/petgame">PTT Pet Game</a>')

    if nadya_count:
        if specific:
            if nadya_count == 1:
                count_str = "a player"
            else:
                count_str = "{} players".format(nadya_count)
            source_chunks.append(
                '{} of <a href="/s/activity/omcs/nadya.jp">nadya.jp</a>'.
                format(count_str))
        else:
            source_chunks.append(
                '<a href="/s/activity/omcs/nadya.jp">nadya.jp</a>')

    source_chunks.extend(sorted(more_sources))
    if len(source_chunks) == 1:
        source_markup = "<strong>Source:</strong> {}".format(source_chunks[0])
    else:
        source_markup = "<strong>Sources:</strong> {}".format(
            oxford_comma(source_chunks))
    return Markup(source_markup)
Exemple #18
0
def script_tag(chunk_id: str) -> str:
    url = url_for('static', filename=manifest[chunk_id])
    return Markup(f'<script type="module" src="{url}"></script>')
Exemple #19
0
def style_tag(style_name: str) -> str:
    url = url_for('static', filename=manifest[style_name + '.css'])
    return Markup(f'<link rel="stylesheet" href="{url}" type="text/css">')