def render_example(site, kw, in_name, out_name): """Render a single .m 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(), MatlabLexer(), 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)
def parse_mfile(mfile, name, path): """ Use Pygments to parse mfile to determine type: function or class. :param mfile: Full path of mfile. :type mfile: str :param name: Name of :class:`MatObject`. :type name: str :param path: Path of module containing :class:`MatObject`. :type path: str :returns: :class:`MatObject` that represents the type of mfile. Assumes that the first token in the file is either one of the keywords: "classdef" or "function" otherwise it is assumed to be a script. """ # use Pygments to parse mfile to determine type: function/classdef # read mfile code with open(mfile, 'r') as code_f: code = code_f.read().replace('\r\n', '\n') # repl crlf with lf # remove the top comment header (if there is one) from the code string code = MatObject._remove_comment_header(code) # functions must be contained in one line, no ellipsis, classdef is OK pat = r"^([^%\n]*)(\.\.\..*\n)" code = re.sub(pat, '\g<1>', code, flags=re.MULTILINE) pat = r"""^[ \t]*function[ \t.\n]* # keyword (function) (\[?[\w, \t.\n]*\]?) # outputs: group(1) [ \t.\n]*=[ \t.\n]* # punctuation (eq) (\w+)[ \t.\n]* # name: group(2) \(?([\w, \t.\n]*)\)?""" # args: group(3) pat = re.compile(pat, re.X | re.MULTILINE) # search start of every line # replacement function def repl(m): retv = m.group(0) # if no args and doesn't end with parentheses, append "()" if not (m.group(3) or m.group(0).endswith('()')): retv = retv.replace(m.group(2), m.group(2) + "()") return retv code = pat.sub(repl, code) # search for functions and apply replacement msg = '[%s] replaced ellipsis & appended parentheses in function signatures' MatObject.sphinx_dbg(msg, MAT_DOM) tks = list(MatlabLexer().get_tokens(code)) # tokenenize code modname = path.replace(os.sep, '.') # module name # assume that functions and classes always start with a keyword if tks[0] == (Token.Keyword, 'function'): MatObject.sphinx_dbg('[%s] parsing function %s from %s.', MAT_DOM, name, modname) return MatFunction(name, modname, tks) elif tks[0] == (Token.Keyword, 'classdef'): MatObject.sphinx_dbg('[%s] parsing classdef %s from %s.', MAT_DOM, name, modname) return MatClass(name, modname, tks) else: # it's a script file return MatScript(name, modname, tks) return None
def viewParameterInSimulation(request, species_wid, wid): #get associated simulation property qs = Parameter.objects.filter(species__wid = species_wid, wid=wid) if not qs[0].state is None: sim_class_name = 'edu.stanford.covert.cell.sim.state.%s' % qs[0].state.wid.replace('State_', '') verbose_class_name = '%s: %s' % (wid, qs[0].state.name) else: sim_class_name = 'edu.stanford.covert.cell.sim.process.%s' % qs[0].process.wid.replace('Process_', '') verbose_class_name = '%s: %s' % (wid, qs[0].process.name) sim_property_name = qs[0].name verbose_property_name = qs[0].name #highlight code for simulation class pathParts = sim_class_name.split('.') codePath = "%s/src/+%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1]) if not os.path.isfile(codePath): codePath = "%s/src/+%s/@%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1], pathParts[-1]) if os.path.isfile(codePath): with open (codePath, "r") as codeFile: code = codeFile.read() lexer = MatlabLexer() lexer.add_filter(PropertyDefinitionFilter(property_names = [sim_property_name], tokentype=Token.Name.Variable)) tokens = lexer.get_tokens(code) object = { 'class_name': sim_class_name, 'property_names': [sim_property_name], 'code': pygments.format(tokens, PygmentsFormatter(linenos='inline', linenostep=1, style=PygmentsStyle, noclasses=True)), } else: raise Http404 #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [Parameter], queryset = qs, templateFile = 'public/viewPropertyInSimulation.html', data = { 'object_list': [object], 'verbose_class_name': verbose_class_name, 'verbose_property_name': verbose_property_name, })
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)
def highlight_source_matlab(filename): """For use inside an IPython notebook: given a filename, print the source code. Matlab version (works with Octave code).""" from pygments import highlight from pygments.lexers import MatlabLexer from pygments.formatters import HtmlFormatter from IPython.core.display import HTML with open(filename, "r") as myfile: data = myfile.read() return HTML(highlight(data, MatlabLexer(), HtmlFormatter(full=True)))
def setup(app): from sphinx.domains.python import PyField from sphinx.util.docfields import Field app.add_css_file('/source/_static/custom.css') app.add_stylesheet('/source/_static/theme_override.css') app.add_object_type('confval', 'confval', objname='configuration value', indextemplate='pair: %s; configuration value', doc_field_types=[ PyField('type', label=_('Type'), has_arg=False, names=('type', ), bodyrolename='class'), Field( 'default', label=_('Default'), has_arg=False, names=('default', ), ), ]) # Patch the MATLAB lexer to correct wrong highlighting from sphinx.highlighting import lexers from pygments.lexers import MatlabLexer from pygments.token import Name from pygments.filters import NameHighlightFilter matlab_lexer = MatlabLexer() matlab_lexer.add_filter( NameHighlightFilter( names=[ 'function', 'end', 'if', 'else', 'elseif', 'switch', 'case', 'return', 'otherwise' ], tokentype=Name.Function, )) app.add_lexer('matlab', matlab_lexer)
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)
def print_code_html(self): highlightbuf = highlight(self.codebuf, MatlabLexer(), HtmlFormatter()) maincontents = [] maincontents.append(self.c.t.hb + self.parsed['name'] + ' - ' + self.parsed['description'] + self.c.t.he) maincontents.append(self.c.t.hb + 'Program code:' + self.c.t.he) maincontents.extend(highlightbuf.split('\n')) if self.c.t.basetype == 'html': return self.structure_as_webpage_html(maincontents, 2) else: return self.structure_as_webpage(maincontents, 2)
def parse_mfile(mfile, name, path): """ Use Pygments to parse mfile to determine type: function or class. :param mfile: Full path of mfile. :type mfile: str :param name: Name of :class:`MatObject`. :type name: str :param path: Path of module containing :class:`MatObject`. :type path: str :returns: :class:`MatObject` that represents the type of mfile. Assumes that the first token in the file is either one of the keywords: "classdef" or "function" otherwise it is assumed to be a script. """ # use Pygments to parse mfile to determine type: function/classdef # read mfile code with open(mfile, 'r') as code_f: code = code_f.read() # functions must be contained in one line, no ellipsis, classdef is OK pat = r"""^[ \t]*(function) # keyword ([\[\], \t\w.\n]*) # outputs ([ \t=.\n]*) # equal sign ([\w.]+) # name \(?([, \t\w.\n]*)\)?""" # args pat = re.compile(pat, re.X | re.MULTILINE) repl = lambda m: m.group().replace('...\n', '') code, nsubs = pat.subn(repl, code) msg = '[%s] replaced %d ellipsis in function signatures' MatObject.sphinx_dbg(msg, MAT_DOM, nsubs) tks = list(MatlabLexer().get_tokens(code)) # tokenenize code modname = path.replace(os.sep, '.') # module name # assume that functions and classes always start with a keyword if tks[0] == (Token.Keyword, 'function'): MatObject.sphinx_dbg('[%s] parsing function %s from %s.', MAT_DOM, name, modname) return MatFunction(name, modname, tks) elif tks[0] == (Token.Keyword, 'classdef'): MatObject.sphinx_dbg('[%s] parsing classdef %s from %s.', MAT_DOM, name, modname) return MatClass(name, modname, tks) else: # it's a script file return MatScript(name, modname, tks) return None
def print_matlab_code(code): print_highlighted_code(code, MatlabLexer())
def viewParametersInSimulation(request, species_wid, wid): obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ verbose_class_name = model._meta.verbose_name verbose_property_name = '%s parameters' % obj.name #get associated simulation property qs = EmptyQuerySet() for field in model._meta.get_all_related_objects() + model._meta.get_all_related_many_to_many_objects(): accessor_name = field.get_accessor_name() if accessor_name == '+': continue if isinstance(field, ForeignKey) and issubclass(field.rel.to, Parameter): qs._result_cache.append(getattr(obj, accessor_name)) if (isinstance(field, ManyToManyField) and issubclass(field.rel.to, Parameter)) or \ (isinstance(field, RelatedObject) and issubclass(field.model, Parameter)): qs2 = getattr(obj, accessor_name).all() if len(qs2) > 0: qs._result_cache.extend(qs2._result_cache) classes = {} for o in qs: if not o.state is None: sim_class_name = 'edu.stanford.covert.cell.sim.state.%s' % o.state.wid.replace('State_', '') else: sim_class_name = 'edu.stanford.covert.cell.sim.process.%s' % o.process.wid.replace('Process_', '') if not classes.has_key(sim_class_name): classes[sim_class_name] = [] classes[sim_class_name].append(o.name) #highlight code for simulation class objects = [] for sim_class_name in classes: sim_property_names = classes[sim_class_name] sim_property_names.sort() pathParts = sim_class_name.split('.') codePath = "%s/src/+%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1]) if not os.path.isfile(codePath): codePath = "%s/src/+%s/@%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1], pathParts[-1]) if not os.path.isfile(codePath): continue with open (codePath, "r") as codeFile: code = codeFile.read() lexer = MatlabLexer() lexer.add_filter(PropertyDefinitionFilter(property_names = sim_property_names, tokentype=Token.Name.Variable)) tokens = lexer.get_tokens(code) objects.append({ 'class_name': sim_class_name, 'property_names': sim_property_names, 'code': pygments.format(tokens, PygmentsFormatter(linenos='inline', linenostep=1, style=PygmentsStyle, noclasses=True)), }) #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [Parameter], queryset = qs, templateFile = 'public/viewPropertyInSimulation.html', data = { 'object_list': objects, 'verbose_class_name': verbose_class_name, 'verbose_property_name': verbose_property_name, })
def viewPropertyInSimulation(request, species_wid, class_name, property_name): #get model model = getModel(class_name) #get verbose class name verbose_class_name = model._meta.verbose_name #get verbose property name verbose_property_name = property_name for fieldset in model._meta.fieldsets: for field in fieldset[1]['fields']: if isinstance(field, (str, unicode)) and field == property_name: tmp = model._meta.get_field_by_name(property_name) if len(tmp) > 0: verbose_property_name = tmp[0].verbose_name if isinstance(field, dict) and field['name'] == property_name: verbose_property_name = field['verbose_name'] #get associated simulation code properties qs = ModelProperty.objects.get( species__wid = species_wid, class_name = class_name, property_name = property_name ).simulation_properties.all().order_by('class_name', 'property_name') #organize simulation code properties by class classes = {} for object in qs: if not classes.has_key(object.class_name): classes[object.class_name] = [] classes[object.class_name].append(object.property_name) #highlight code for each simulation class object_list = [] for sim_class_name in classes: sim_property_names = classes[sim_class_name] sim_property_names.sort() pathParts = sim_class_name.split('.') codePath = "%s/src/+%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1]) if not os.path.isfile(codePath): codePath = "%s/src/+%s/@%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1], pathParts[-1]) if not os.path.isfile(codePath): continue with open (codePath, "r") as codeFile: code = codeFile.read() lexer = MatlabLexer() lexer.add_filter(PropertyDefinitionFilter(property_names = sim_property_names, tokentype=Token.Name.Variable)) tokens = lexer.get_tokens(code) object_list.append({ 'class_name': sim_class_name, 'property_names': sim_property_names, 'code': pygments.format(tokens, PygmentsFormatter(linenos='inline', linenostep=1, style=PygmentsStyle, noclasses=True)), }) #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [SimulationProperty], queryset = qs, templateFile = 'public/viewPropertyInSimulation.html', data = { 'object_list': object_list, 'verbose_class_name': verbose_class_name, 'verbose_property_name': verbose_property_name, })
def lexer(): yield MatlabLexer()