Ejemplo n.º 1
0
        def render_listing(in_name, out_name, input_folder, output_folder):
            needs_ipython_css = False
            if in_name.endswith('.ipynb'):
                # Special handling: render ipynbs in listings (Issue #1900)
                ipynb_compiler = self.site.plugin_manager.getPluginByName(
                    "ipynb", "PageCompiler").plugin_object  # NOQA: E501
                with io.open(in_name, "r", encoding="utf8") as in_file:
                    nb_json = ipynb_compiler._nbformat_read(in_file)
                    ipynb_raw = ipynb_compiler._compile_string(nb_json)
                ipynb_html = lxml.html.fromstring(ipynb_raw)
                # The raw HTML contains garbage (scripts and styles), we can’t leave it in
                code = lxml.html.tostring(ipynb_html, encoding='unicode')
                title = os.path.basename(in_name)
                needs_ipython_css = True
            elif in_name.endswith('.m'):
                lexer = MatlabLexer()
                with open(in_name, 'r') as fd:
                    code = highlight(fd.read(), lexer,
                                     utils.NikolaPygmentsHTML(in_name))
                title = os.path.basename(in_name)
            else:
                with open(in_name, 'r') as fd:
                    try:
                        lexer = get_lexer_for_filename(in_name)
                    except Exception:
                        try:
                            lexer = guess_lexer(fd.read())
                        except Exception:
                            lexer = TextLexer()
                        fd.seek(0)
                    code = highlight(fd.read(), lexer,
                                     utils.NikolaPygmentsHTML(in_name))
                title = os.path.basename(in_name)

            permalink = self.site.link(
                'listing',
                os.path.join(
                    input_folder,
                    os.path.relpath(
                        out_name[:-5],  # remove '.html'
                        os.path.join(self.kw['output_folder'],
                                     output_folder))))
            source_link = permalink[:-5]  # remove '.html'
            context = {
                'code': code,
                'title': title,
                'permalink': permalink,
                'lang': self.kw['default_lang'],
                'description': title,
                'source_link': source_link,
                'pagekind': ['listing'],
            }
            if needs_ipython_css:
                # If someone does not have ipynb posts and only listings, we
                # need to enable ipynb CSS for ipynb listings.
                context['needs_ipython_css'] = True
            self.site.render_template('listing.tmpl', out_name, context)
Ejemplo n.º 2
0
        def render_listing(in_name, out_name, input_folder, output_folder):
            needs_ipython_css = False
            if in_name.endswith(".ipynb"):
                # Special handling: render ipynb in listings (Issue #1900)
                ipynb_compiler = self.site.plugin_manager.getPluginByName(
                    "ipynb", "PageCompiler").plugin_object
                with io.open(in_name, "r", encoding="utf8") as in_file:
                    nb_json = ipynb_compiler._nbformat_read(in_file)
                    ipynb_raw = ipynb_compiler._compile_string(nb_json)
                ipynb_html = lxml.html.fromstring(ipynb_raw)
                code = lxml.html.tostring(ipynb_html, encoding="unicode")
                needs_ipython_css = True
            elif in_name.endswith(".m"):
                lexer = MatlabLexer()
                with open(in_name, "r") as fd:
                    code = highlight(fd.read(), lexer,
                                     utils.NikolaPygmentsHTML(in_name))
            else:
                with open(in_name, "r") as fd:
                    try:
                        lexer = get_lexer_for_filename(in_name)
                    except Exception:
                        try:
                            lexer = guess_lexer(fd.read())
                        except Exception:
                            lexer = TextLexer()
                        fd.seek(0)
                    code = highlight(fd.read(), lexer,
                                     utils.NikolaPygmentsHTML(in_name))

            title = os.path.basename(in_name)

            permalink = os.path.relpath(out_name, self.kw["output_folder"])
            source_link = os.path.basename(permalink)[:-5]  # remove '.html'
            context = {
                "code": code,
                "title": title,
                "permalink": permalink,
                "lang": self.kw["default_lang"],
                "description": title,
                "source_link": source_link,
                "pagekind": ["example"],
            }
            if needs_ipython_css:
                # If someone does not have ipynb posts and only listings, we
                # need to enable ipynb CSS for ipynb listings.
                context["needs_ipython_css"] = True
            self.site.render_template("listing.tmpl", out_name, context)
Ejemplo n.º 3
0
    def run(self):
        self.assert_has_content()

        if 'linenos' in self.options:
            self.options['number-lines'] = self.options['linenos']
        if 'tab-width' in self.options:
            self.content = [
                x.replace('\t', ' ' * self.options['tab-width'])
                for x in self.content
            ]

        if self.arguments:
            language = self.arguments[0]
        else:
            language = 'text'
        set_classes(self.options)
        classes = ['code']
        if language:
            classes.append(language)
        if 'classes' in self.options:
            classes.extend(self.options['classes'])

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

        try:
            lexer = get_lexer_by_name(language)
        except pygments.util.ClassNotFound:
            raise self.error(
                'Cannot find pygments lexer for language "{0}"'.format(
                    language))

        if 'number-lines' in self.options:
            linenos = 'table'
            # optional argument `startline`, defaults to 1
            try:
                linenostart = int(self.options['number-lines'] or 1)
            except ValueError:
                raise self.error(':number-lines: with non-integer start value')
        else:
            linenos = False
            linenostart = 1  # actually unused

        if self.site.invariant:  # for testing purposes
            anchor_ref = 'rest_code_' + 'fixedvaluethatisnotauuid'
        else:
            anchor_ref = 'rest_code_' + uuid.uuid4().hex

        formatter = utils.NikolaPygmentsHTML(anchor_ref=anchor_ref,
                                             classes=classes,
                                             linenos=linenos,
                                             linenostart=linenostart)
        out = pygments.highlight(code, lexer, formatter)
        node = nodes.raw('', out, format='html')

        self.add_name(node)
        # if called from "include", set the source
        if 'source' in self.options:
            node.attributes['source'] = self.options['source']

        return [node]
Ejemplo n.º 4
0
def render_example(site, kw, in_names, out_name):
    """Render a set of .h and .cpp files to HTML with formatting.

    Parameters
    ==========
    site:
        An instance of a Nikola site, available in any plugin as ``self.site``
    kw:
        A dictionary of keywords for this task
    in_names:
        The files to be rendered, as a list of pathlib.Path objects
    out_name:
        A pathlib.Path instance pointing to the rendered output file

    """
    items = []
    for source_file in in_names:
        code = highlight(source_file.read_bytes(), CppLexer(),
                         utils.NikolaPygmentsHTML(source_file.name))
        items.append({
            "code": code,
            "title": source_file.name,
            "source_link": source_file.name,
        })

    context = {
        "items": items,
        "title": out_name,
        "lang": kw["default_lang"],
        "pagekind": ["example"],
    }
    site.render_template("multifile-example.tmpl", str(out_name), context)
def render_example(site, kw, in_name, out_name):
    """Render a single .py file to HTML with formatting.

    Parameters
    ==========
    site:
        An instance of a Nikola site, available in any plugin as ``self.site``
    kw:
        A dictionary of keywords for this task
    in_name:
        The file to be rendered, as an instance of pathlib.Path
    out_name:
        A pathlib.Path instance pointing to the rendered output file

    """
    code = highlight(
        in_name.read_bytes(), Python3Lexer(), utils.NikolaPygmentsHTML(in_name.name)
    )

    title = in_name.name

    permalink = out_name.relative_to(kw["output_folder"])
    source_link = permalink.stem  # remove '.html'
    context = {
        "code": code,
        "title": title,
        "permalink": str(permalink),
        "lang": kw["default_lang"],
        "description": title,
        "source_link": source_link,
        "pagekind": ["example"],
    }
    site.render_template("examples.tmpl", str(out_name), context)
Ejemplo n.º 6
0
 def render_listing(in_name, out_name, input_folder, output_folder, folders=[], files=[]):
     needs_ipython_css = False
     if in_name and in_name.endswith('.ipynb'):
         # Special handling: render ipynbs in listings (Issue #1900)
         ipynb_compiler = self.site.plugin_manager.getPluginByName("ipynb", "PageCompiler").plugin_object
         ipynb_raw = ipynb_compiler.compile_html_string(in_name, True)
         ipynb_html = lxml.html.fromstring(ipynb_raw)
         # The raw HTML contains garbage (scripts and styles), we can’t leave it in
         code = lxml.html.tostring(ipynb_html.xpath('//*[@id="notebook"]')[0], encoding='unicode')
         title = os.path.basename(in_name)
         needs_ipython_css = True
     elif in_name:
         with open(in_name, 'r') as fd:
             try:
                 lexer = get_lexer_for_filename(in_name)
             except:
                 lexer = TextLexer()
             code = highlight(fd.read(), lexer, utils.NikolaPygmentsHTML(in_name))
         title = os.path.basename(in_name)
     else:
         code = ''
         title = os.path.split(os.path.dirname(out_name))[1]
     crumbs = utils.get_crumbs(os.path.relpath(out_name,
                                               self.kw['output_folder']),
                               is_file=True)
     permalink = self.site.link(
         'listing',
         os.path.join(
             input_folder,
             os.path.relpath(
                 out_name[:-5],  # remove '.html'
                 os.path.join(
                     self.kw['output_folder'],
                     output_folder))))
     if self.site.config['COPY_SOURCES'] and in_name:
         source_link = permalink[:-5]  # remove '.html'
     else:
         source_link = None
     context = {
         'code': code,
         'title': title,
         'crumbs': crumbs,
         'permalink': permalink,
         'lang': self.kw['default_lang'],
         'folders': natsort.natsorted(
             folders, alg=natsort.ns.F | natsort.ns.IC),
         'files': natsort.natsorted(
             files, alg=natsort.ns.F | natsort.ns.IC),
         'description': title,
         'source_link': source_link,
         'pagekind': ['listing'],
     }
     if needs_ipython_css:
         # If someone does not have ipynb posts and only listings, we
         # need to enable ipynb CSS for ipynb listings.
         context['needs_ipython_css'] = True
     self.site.render_template('listing.tmpl', out_name, context)
Ejemplo n.º 7
0
def parse_theme_info(post, pkg_dir, config):
    theme = os.path.basename(pkg_dir)
    data = {}
    data['name'] = theme
    out_path = post.folder_relative + '/' + theme
    demo_dir = config['demo_screenshots_map'].get(out_path, out_path)
    data['previewimage'] = '/' + demo_dir + '.png'
    data['previewimage_thumbnail'] = '/' + demo_dir + '.thumbnail.png'
    data['demo_link'] = '/' + demo_dir + '/demo/'
    conf_sample = os.path.join(pkg_dir, 'conf.py.sample')
    engine = os.path.join(pkg_dir, 'engine')
    parent = os.path.join(pkg_dir, 'parent')

    if os.path.exists(conf_sample):
        post.add_dependency(conf_sample)
        with io.open(conf_sample, 'r', encoding='utf-8') as f:
            data['confpy'] = pygments.highlight(
                f.read(),
                PythonLexer(), utils.NikolaPygmentsHTML(theme + '_conf_sample', linenos=False))
    else:
        data['confpy'] = None

    if os.path.exists(engine):
        post.add_dependency(engine)
        with io.open(engine, 'r', encoding='utf-8') as f:
            data['engine'] = f.read().strip()
    else:
        data['engine'] = 'mako'

    if os.path.exists(parent):
        post.add_dependency(parent)
        with io.open(parent, 'r', encoding='utf-8') as f:
            data['parent'] = f.read().strip()
    elif theme == 'base':
        pass
    else:
        raise ValueError("Theme {0} has no parent.".format(theme))

    data['chain'] = utils.get_theme_chain(theme, [os.path.dirname(pkg_dir), 'themes'])
    data['chain'] = [os.path.basename(i) for i in reversed(data['chain'])]
    data['bootswatch'] = (('bootstrap' in data['chain'] or
                           'bootstrap-jinja' in data['chain'] or
                           'bootstrap3-jinja' in data['chain'] or
                           'bootstrap3' in data['chain']) and
                          'bootstrap3-gradients' not in data['chain'])
    data['tags'] = 'theme,' + data['engine']

    return data
Ejemplo n.º 8
0
 def render_listing(in_name, out_name, input_folder, output_folder, folders=[], files=[]):
     if in_name:
         with open(in_name, 'r') as fd:
             try:
                 lexer = get_lexer_for_filename(in_name)
             except:
                 lexer = TextLexer()
             code = highlight(fd.read(), lexer, utils.NikolaPygmentsHTML(in_name))
         title = os.path.basename(in_name)
     else:
         code = ''
         title = os.path.split(os.path.dirname(out_name))[1]
     crumbs = utils.get_crumbs(os.path.relpath(out_name,
                                               self.kw['output_folder']),
                               is_file=True)
     permalink = self.site.link(
         'listing',
         os.path.join(
             input_folder,
             os.path.relpath(
                 out_name[:-5],  # remove '.html'
                 os.path.join(
                     self.kw['output_folder'],
                     output_folder))))
     if self.site.config['COPY_SOURCES'] and in_name:
         source_link = permalink[:-5]  # remove '.html'
     else:
         source_link = None
     context = {
         'code': code,
         'title': title,
         'crumbs': crumbs,
         'permalink': permalink,
         'lang': self.kw['default_lang'],
         'folders': natsort.natsorted(
             folders, alg=natsort.ns.F | natsort.ns.IC),
         'files': natsort.natsorted(
             files, alg=natsort.ns.F | natsort.ns.IC),
         'description': title,
         'source_link': source_link,
     }
     self.site.render_template('listing.tmpl', out_name, context)
Ejemplo n.º 9
0
def parse_plugin_file(post, pkg_dir, config):
    plugin = os.path.basename(pkg_dir)
    data = {}
    data['name'] = plugin
    conf_sample = os.path.join(pkg_dir, 'conf.py.sample')
    ini = os.path.join(pkg_dir, plugin + '.plugin')
    reqpy = os.path.join(pkg_dir, 'requirements.txt')
    reqnonpy = os.path.join(pkg_dir, 'requirements-nonpy.txt')
    reqplugins = os.path.join(pkg_dir, 'requirements-plugins.txt')

    if os.path.exists(ini):
        post.add_dependency(ini)
        try:
            c = configparser.ConfigParser()
            c.read(ini)
            data['author'] = c.get('Documentation', 'Author')
            data['version'] = c.get('Documentation', 'Version')
            data['description'] = c.get('Documentation', 'Description')
            data['website'] = _cp_try_get(c, 'Documentation', 'Website')
            data['minver'] = _cp_try_get(c, 'Nikola', 'MinVersion')
            data['maxver'] = _cp_try_get(c, 'Nikola', 'MaxVersion')

            category = c.get('Nikola', 'PluginCategory')
            data['category'] = category

            if data['minver']:
                minver = data['minver'].split('.')[0]
            else:
                minver = _version_from_path(pkg_dir)
                data['minver'] = minver
            if data['maxver']:
                maxver = data['maxver'].split('.')[0]
            else:
                maxver = config['versions_supported'][-1]

            data['allver'] = list(range(int(minver), int(maxver) + 1))

            tags = ['plugin', category]
            if category == 'CompilerExtension':
                data['compiler'] = c.get('Nikola', 'Compiler')
                tags.append(data['compiler'])
            for version in data['allver']:
                tags.append('v{0}'.format(version))
            data['tags'] = ','.join(tags)

            data['tests'] = _cp_try_get(c, 'Core', 'Tests')
        except Exception as e:
            raise Exception('Exception while reading plugin config "{0}": {1}'.format(ini, e)) from e
    else:
        raise ValueError('Plugin {0} has no .plugin file in the main '
                         'directory.'.format(plugin))

    if os.path.exists(conf_sample):
        post.add_dependency(conf_sample)
        with io.open(conf_sample, 'r', encoding='utf-8') as f:
            data['confpy'] = pygments.highlight(
                f.read(),
                PythonLexer(), utils.NikolaPygmentsHTML(plugin + '_conf_sample', linenos=False))
    else:
        data['confpy'] = None

    if os.path.exists(reqpy):
        post.add_dependency(reqpy)
        with io.open(reqpy, 'r', encoding='utf-8') as f:
            data['pyreqs'] = f.readlines()
    else:
        data['pyreqs'] = []

    if os.path.exists(reqnonpy):
        post.add_dependency(reqnonpy)
        with io.open(reqnonpy, 'r', encoding='utf-8') as f:
            r = f.readlines()

        data['nonpyreqs'] = [i.strip().split('::') for i in r]
    else:
        data['nonpyreqs'] = []

    if os.path.exists(reqplugins):
        post.add_dependency(reqplugins)
        with io.open(reqplugins, 'r', encoding='utf-8') as f:
            data['pluginreqs'] = f.readlines()
    else:
        data['pluginreqs'] = []

    data['reqcount'] = len(data['pyreqs']) + len(data['nonpyreqs']) + len(data['pluginreqs'])

    return data
Ejemplo n.º 10
0
        def render_listing(in_name, out_name, input_folder, output_folder, folders=[], files=[]):
            needs_ipython_css = False
            if in_name and in_name.endswith('.ipynb'):
                # Special handling: render ipynbs in listings (Issue #1900)
                ipynb_plugin = self.site.plugin_manager.getPluginByName("ipynb", "PageCompiler")
                if ipynb_plugin is None:
                    msg = "To use .ipynb files as listings, you must set up the Jupyter compiler in COMPILERS and POSTS/PAGES."
                    utils.LOGGER.error(msg)
                    raise ValueError(msg)

                ipynb_compiler = ipynb_plugin.plugin_object
                with open(in_name, "r", encoding="utf8") as in_file:
                    nb_json = ipynb_compiler._nbformat_read(in_file)
                    code = ipynb_compiler._compile_string(nb_json)
                title = os.path.basename(in_name)
                needs_ipython_css = True
            elif in_name:
                with open(in_name, 'r', encoding='utf-8') as fd:
                    try:
                        lexer = get_lexer_for_filename(in_name)
                    except Exception:
                        try:
                            lexer = guess_lexer(fd.read())
                        except Exception:
                            lexer = TextLexer()
                        fd.seek(0)
                    code = highlight(
                        fd.read(), lexer,
                        utils.NikolaPygmentsHTML(in_name, linenos='table'))
                title = os.path.basename(in_name)
            else:
                code = ''
                title = os.path.split(os.path.dirname(out_name))[1]
            crumbs = utils.get_crumbs(os.path.relpath(out_name,
                                                      self.kw['output_folder']),
                                      is_file=True)
            permalink = self.site.link(
                'listing',
                os.path.join(
                    input_folder,
                    os.path.relpath(
                        out_name[:-5],  # remove '.html'
                        os.path.join(
                            self.kw['output_folder'],
                            output_folder))))
            if in_name:
                source_link = permalink[:-5]  # remove '.html'
            else:
                source_link = None
            context = {
                'code': code,
                'title': title,
                'crumbs': crumbs,
                'permalink': permalink,
                'lang': self.kw['default_lang'],
                'folders': natsort.natsorted(
                    folders, alg=natsort.ns.F | natsort.ns.IC),
                'files': natsort.natsorted(
                    files, alg=natsort.ns.F | natsort.ns.IC),
                'description': title,
                'source_link': source_link,
                'pagekind': ['listing'],
            }
            if needs_ipython_css:
                # If someone does not have ipynb posts and only listings, we
                # need to enable ipynb CSS for ipynb listings.
                context['needs_ipython_css'] = True
            self.site.render_template('listing.tmpl', out_name, context)
Ejemplo n.º 11
0
def parse_theme_info(post, pkg_dir, config):
    theme = os.path.basename(pkg_dir)
    data = {}
    data['name'] = theme
    out_path = post.folder_relative + '/' + theme
    demo_dir = config['demo_screenshots_map'].get(out_path, out_path)
    data['previewimage'] = '/' + demo_dir + '.png'
    data['previewimage_thumbnail'] = '/' + demo_dir + '.thumbnail.png'
    data['demo_link'] = '/' + demo_dir + '/demo/'
    conf_sample = os.path.join(pkg_dir, 'conf.py.sample')
    ini = os.path.join(pkg_dir, theme + '.theme')
    engine = os.path.join(pkg_dir, 'engine')
    parent = os.path.join(pkg_dir, 'parent')

    data['chain'] = utils.get_theme_chain(theme,
                                          [os.path.dirname(pkg_dir), 'themes'])
    data['chain'] = [os.path.basename(i) for i in reversed(data['chain'])]

    if os.path.exists(conf_sample):
        post.add_dependency(conf_sample)
        with io.open(conf_sample, 'r', encoding='utf-8') as f:
            data['confpy'] = pygments.highlight(
                f.read(), PythonLexer(),
                utils.NikolaPygmentsHTML(theme + '_conf_sample',
                                         linenos=False))
    else:
        data['confpy'] = None

    if os.path.exists(ini):
        post.add_dependency(ini)
        c = configparser.ConfigParser()
        c.read(ini)
        data['parent'] = c.get('Theme', 'parent', fallback=None)
        data['engine'] = c.get('Theme', 'engine', fallback='mako')
        data['family'] = c.get('Family', 'family', fallback=theme)
        data['family_head'] = data['family'] == theme
        if data['engine'] == 'mako':
            data['family_mako_version'] = theme
            _other = c.get('Family', 'jinja_version', fallback=None)
            data['family_jinja_version'] = _other
            data['show_family_data'] = bool(_other)
        else:
            data['family_jinja_version'] = theme
            _other = c.get('Family', 'mako_version', fallback=None)
            data['family_mako_version'] = _other
            data['show_family_data'] = bool(_other)

        data['family_variants'] = [
            i.strip()
            for i in c.get('Family', 'variants', fallback='').split(',')
        ]
        data['family_variants'] = [i for i in data['family_variants']
                                   if i]  # remove empty strings
        _variants_count = len(data['family_variants'])
        data[
            'family_variants_text'] = '1 variant' if _variants_count == 1 else '{0} variants'.format(
                _variants_count)

        data['show_family_data'] = data['show_family_data'] or bool(
            data['family_variants'])
        data['bootswatch'] = c.getboolean('Nikola',
                                          'bootswatch',
                                          fallback=False)
        data['tags'] = 'newmeta,theme,' + data['engine']
        data['license'] = c.get('Theme', 'license', fallback=None)
        data['author'] = c.get('Theme', 'author', fallback=None)
        data['author_url'] = c.get('Theme', 'author_url', fallback=None)
        data['author_url'] = c.get('Theme', 'author_url', fallback=None)
        based_on = c.get('Theme', 'based_on', fallback=None)
        based_on_list = []
        if based_on:
            for i in based_on.split(','):
                i = i.strip()
                m = re.match("(.*?) ?<(.*?)>", i)
                if m:
                    based_on_list.append('<a href="{0[1]}">{0[0]}</a>'.format(
                        m.groups()))
                else:
                    based_on_list.append(i)

        data['based_on'] = ', '.join(based_on_list)

        theme_tags = c.get('Theme', 'tags', fallback='')
        if theme_tags:
            data['tags'] += ',' + theme_tags

        if data['parent'] is None and theme != 'base':
            raise ValueError("Theme {0} has no parent.".format(theme))
    else:
        # Old-style metadata
        if os.path.exists(engine):
            post.add_dependency(engine)
            with io.open(engine, 'r', encoding='utf-8') as f:
                data['engine'] = f.read().strip()
        else:
            data['engine'] = 'mako'

        if os.path.exists(parent):
            post.add_dependency(parent)
            with io.open(parent, 'r', encoding='utf-8') as f:
                data['parent'] = f.read().strip()
        elif theme == 'base':
            pass
        else:
            raise ValueError("Theme {0} has no parent.".format(theme))
        data['bootswatch'] = (('bootstrap' in data['chain']
                               or 'bootstrap-jinja' in data['chain']
                               or 'bootstrap3-jinja' in data['chain']
                               or 'bootstrap3' in data['chain'])
                              and 'bootstrap3-gradients' not in data['chain'])
        data['tags'] = 'theme,' + data['engine']
        data['family'] = theme
        data['family_head'] = True
        data['family_{0}_version'.format(data['engine'])] = theme

    return data
Ejemplo n.º 12
0
    def run(self):
        """Run code block directive."""
        self.assert_has_content()

        if 'linenos' in self.options:
            self.options['number-lines'] = self.options['linenos']
        if 'tab-width' in self.options:
            self.content = [
                x.replace('\t', ' ' * self.options['tab-width'])
                for x in self.content
            ]

        if self.arguments:
            language = self.arguments[0]
        else:
            language = 'text'
        set_classes(self.options)
        classes = ['code']
        if language:
            classes.append(language)
        if 'classes' in self.options:
            classes.extend(self.options['classes'])

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

        try:
            lexer = get_lexer_by_name(language)
        except pygments.util.ClassNotFound:
            raise self.error(
                'Cannot find pygments lexer for language "{0}"'.format(
                    language))

        if 'number-lines' in self.options:
            linenos = 'table'
            # optional argument `startline`, defaults to 1
            try:
                linenostart = int(self.options['number-lines'] or 1)
            except ValueError:
                raise self.error(':number-lines: with non-integer start value')
        else:
            linenos = False
            linenostart = 1  # actually unused

        if self.site.invariant:  # for testing purposes
            anchor_ref = 'rest_code_' + 'fixedvaluethatisnotauuid'
        else:
            anchor_ref = 'rest_code_' + uuid.uuid4().hex

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = utils.parselinenos(linespec, nlines)
                if any(i >= nlines for i in hl_lines):
                    raise self.error(
                        'line number spec is out of range(1-%d): %r' %
                        (nlines, self.options['emphasize-lines']))
                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                raise self.error(err)
        else:
            hl_lines = None

        extra_kwargs = {}
        if hl_lines is not None:
            extra_kwargs['hl_lines'] = hl_lines

        formatter = utils.NikolaPygmentsHTML(anchor_ref=anchor_ref,
                                             classes=classes,
                                             linenos=linenos,
                                             linenostart=linenostart,
                                             **extra_kwargs)
        out = pygments.highlight(code, lexer, formatter)
        node = nodes.raw('', out, format='html')

        self.add_name(node)
        # if called from "include", set the source
        if 'source' in self.options:
            node.attributes['source'] = self.options['source']

        return [node]