Python pygments.lexers.guess_lexer() Examples
The following are 24
code examples of pygments.lexers.guess_lexer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygments.lexers
, or try the search function
.
Example #1
Source File: pdfbuilder.py From rst2pdf with MIT License | 6 votes |
def lang_for_block(source, lang): if lang in ('py', 'python'): if source.startswith('>>>'): # interactive session return 'pycon' else: # maybe Python -- try parsing it if try_parse(source): return 'python' else: # Guess return lang_for_block(source, 'guess') elif lang in ('python3', 'py3') and source.startswith('>>>'): # for py3, recognize interactive sessions, but do not try parsing... return 'pycon3' elif lang == 'guess': try: # return 'python' lexer = guess_lexer(source) return lexer.aliases[0] except Exception: return None else: return lang
Example #2
Source File: highlight.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
def get_lexer(self, src, language): """Get the Pygments lexer.""" if language: language, lexer_options = self.get_extended_language(language) else: lexer_options = {} # Try and get lexer by the name given. try: lexer = get_lexer_by_name(language, **lexer_options) except Exception: lexer = None if lexer is None: if self.guess_lang: try: lexer = guess_lexer(src) except Exception: # pragma: no cover pass if lexer is None: lexer = get_lexer_by_name('text') return lexer
Example #3
Source File: highlight.py From sublime-markdown-popups with MIT License | 6 votes |
def get_lexer(self, src, language): """Get the Pygments lexer.""" if language: language, lexer_options = self.get_extended_language(language) else: lexer_options = {} # Try and get lexer by the name given. try: lexer = get_lexer_by_name(language, **lexer_options) except Exception: lexer = None if lexer is None: if self.guess_lang: try: lexer = guess_lexer(src) except Exception: # pragma: no cover pass if lexer is None: lexer = get_lexer_by_name('text') return lexer
Example #4
Source File: test_cpp.py From pygments with BSD 2-Clause "Simplified" License | 6 votes |
def test_guess_c_lexer(): code = ''' #include <stdio.h> #include <stdlib.h> int main(void); int main(void) { uint8_t x = 42; uint8_t y = x + 1; /* exit 1 for success! */ return 1; } ''' lexer = guess_lexer(code) assert isinstance(lexer, CLexer)
Example #5
Source File: utility.py From pinnwand with MIT License | 5 votes |
def guess_language(raw: str, filename: Optional[str] = None) -> str: options = {'stripnl': True} # Guess a lexer based on filename and raw text first if filename: try: return guess_lexer_for_filename(filename, raw, **options).aliases[0] except (ValueError, IndexError): pass # If that didn't work guess lexer just by looking at the raw text try: language = guess_lexer(raw, **options).aliases[0] except (ValueError, IndexError): # If no lexer was detected, fallback to plain text. return 'text' # These are odd lexers that match far too often, so exclude them. if language in GUESS_LANG_IGNORES: return 'text' # Finally check for language overrides and return return GUESS_LANG_OVERRIDES.get(language, language)
Example #6
Source File: textfmts.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
def _highlight_code(self, match): code = match.group(1) try: if self.body_lexer: lexer = get_lexer_by_name(self.body_lexer) else: lexer = guess_lexer(code.strip()) except ClassNotFound: lexer = get_lexer_by_name('text') for item in lexer.get_tokens_unprocessed(code): yield item
Example #7
Source File: st_pygments_highlight.py From sublime-markdown-popups with MIT License | 5 votes |
def syntax_hl(src, lang=None, guess_lang=False, inline=False, code_wrap=False): """Highlight.""" css_class = 'highlight' src = src.strip('\n') try: lexer = get_lexer_by_name(lang) except ValueError: try: if guess_lang: lexer = guess_lexer(src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') if inline: formatter = SublimeInlineHtmlFormatter( cssclass=css_class ) elif code_wrap: formatter = SublimeWrapBlockFormatter( cssclass=css_class ) else: formatter = SublimeBlockFormatter( cssclass=css_class ) return highlight(src, lexer, formatter)
Example #8
Source File: lxml_code.py From enaml-web with MIT License | 5 votes |
def _default_lexer(self): d = self.declaration if d.language: return lexers.find_lexer_class_by_name(d.language)() return lexers.guess_lexer(d.source)
Example #9
Source File: detect_language_from_file.py From repo_info_extractor with MIT License | 5 votes |
def detect_language_from_file(file_path): if os.path.exists(file_path): try: with open(file_path, 'r') as file: code = file.read() language = guess_lexer(code) if language.name == 'Objective-C': return language.name else: return "MATLAB" except: return None
Example #10
Source File: highlight.py From microblog.pub with GNU Affero General Public License v3.0 | 5 votes |
def highlight(html: str) -> str: soup = BeautifulSoup(html, "html5lib") for code in soup.find_all("code"): if not code.parent.name == "pre": continue lexer = guess_lexer(code.text) tag = BeautifulSoup( phighlight(code.text, lexer, _FORMATTER), "html5lib" ).body.next pre = code.parent pre.replaceWith(tag) out = soup.body out.name = "div" return str(out)
Example #11
Source File: test_basic_api.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def test_get_lexers(): # test that the lexers functions work for func, args in [(lexers.get_lexer_by_name, ("python",)), (lexers.get_lexer_for_filename, ("test.py",)), (lexers.get_lexer_for_mimetype, ("text/x-python",)), (lexers.guess_lexer, ("#!/usr/bin/python3 -O\nprint",)), (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>")) ]: x = func(opt='val', *args) assert isinstance(x, lexers.PythonLexer) assert x.options["opt"] == "val" for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items(): assert cls == lexers.find_lexer_class(lname).__name__ for alias in aliases: assert cls == lexers.get_lexer_by_name(alias).__class__.__name__ for mimetype in mimetypes: assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__ try: lexers.get_lexer_by_name(None) except ClassNotFound: pass else: raise Exception
Example #12
Source File: textfmts.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
def _highlight_code(self, match): code = match.group(1) try: if self.body_lexer: lexer = get_lexer_by_name(self.body_lexer) else: lexer = guess_lexer(code.strip()) except ClassNotFound: lexer = get_lexer_by_name('text') for item in lexer.get_tokens_unprocessed(code): yield item
Example #13
Source File: code2pdf.py From python-automation-scripts with GNU General Public License v3.0 | 5 votes |
def highlight_file(self, linenos=True, style='default'): """ Highlight the input file, and return HTML as a string. """ try: lexer = lexers.get_lexer_for_filename(self.input_file) except pygments.util.ClassNotFound: # Try guessing the lexer (file type) later. lexer = None try: formatter = formatters.HtmlFormatter( linenos=linenos, style=style, full=True) except pygments.util.ClassNotFound: logging.error("\nInvalid style name: {}\nExpecting one of:\n \ {}".format(style, "\n ".join(sorted(styles.STYLE_MAP)))) sys.exit(1) try: with open(self.input_file, "r") as f: content = f.read() try: lexer = lexer or lexers.guess_lexer(content) except pygments.util.ClassNotFound: # No lexer could be guessed. lexer = lexers.get_lexer_by_name("text") except EnvironmentError as exread: fmt = "\nUnable to read file: {}\n{}" logging.error(fmt.format(self.input_file, exread)) sys.exit(2) return pygments.highlight(content, lexer, formatter)
Example #14
Source File: pygments_renderer.py From mistletoe with MIT License | 5 votes |
def render_block_code(self, token): code = token.children[0].content lexer = get_lexer(token.language) if token.language else guess_lexer(code) return highlight(code, lexer, self.formatter)
Example #15
Source File: spider.py From vy with MIT License | 5 votes |
def set_lexer(self): """ Try to detect the lexer by filename if it fails then try to guess the lex by shebang statement. The shebang statement should be placed in the first 20 lines of the file. """ try: self.lexer = get_lexer_for_filename(self.area.filename, '') except Exception as e: self.lexer = guess_lexer(self.area.get('1.0', '20.0'))
Example #16
Source File: codehilite.py From marko with MIT License | 5 votes |
def render_fenced_code(self, element): code = element.children[0].children options = CodeHiliteRendererMixin.options.copy() options.update(_parse_extras(getattr(element, "extra", None))) if element.lang: try: lexer = get_lexer_by_name(element.lang, stripall=True) except ClassNotFound: lexer = guess_lexer(code) else: lexer = guess_lexer(code) formatter = html.HtmlFormatter(**options) return highlight(code, lexer, formatter)
Example #17
Source File: code2pdf.py From code2pdf with MIT License | 5 votes |
def highlight_file(self, linenos=True, style='default'): """ Highlight the input file, and return HTML as a string. """ try: lexer = lexers.get_lexer_for_filename(self.input_file) except pygments.util.ClassNotFound: # Try guessing the lexer (file type) later. lexer = None try: formatter = formatters.HtmlFormatter( linenos=linenos, style=style, full=True) except pygments.util.ClassNotFound: logging.error("\nInvalid style name: {}\nExpecting one of:\n \ {}".format(style, "\n ".join(sorted(styles.STYLE_MAP)))) sys.exit(1) try: with open(self.input_file, "r") as f: content = f.read() try: lexer = lexer or lexers.guess_lexer(content) except pygments.util.ClassNotFound: # No lexer could be guessed. lexer = lexers.get_lexer_by_name("text") except EnvironmentError as exread: fmt = "\nUnable to read file: {}\n{}" logging.error(fmt.format(self.input_file, exread)) sys.exit(2) return pygments.highlight(content, lexer, formatter)
Example #18
Source File: code.py From beampy with GNU General Public License v3.0 | 4 votes |
def code2svg(self): """ function to render code to svg """ inkscapecmd = document._external_cmd['inkscape'] codein = self.content #Try to infer the lexer if self.language is None: lexer = guess_lexer(codein) else: lexer = get_lexer_by_name(self.language, stripall=True) #Convert code to svgfile svgcode = highlight(codein, lexer, SvgFormatter(fontsize=self.font_size, style='tango')) # Use tempfile.NamedTemporaryFile, that automaticly delete the file on close by default with tempfile.NamedTemporaryFile(mode='w', suffix='.svg', prefix='beampytmp_CODE') as f: tmpname, tmpext = os.path.splitext(f.name) f.write(svgcode) # Need to flush the file to write it to disk f.file.flush() #Convert svgfile with inkscape to transform text to path cmd = inkscapecmd + ' -z -T -l=%s %s'%(tmpname+'_good.svg', f.name) req = os.popen(cmd) req.close() f = figure(tmpname+'_good.svg', width=self.width.value, height=self.height.value) f.positionner = self.positionner f.render() self.svgout = f.svgout self.positionner = f.positionner self.width = f.width self.height = f.height self.update_size(self.width, self.height) f.delete() #remove files os.remove(tmpname+'_good.svg')
Example #19
Source File: syntax_highlighting.py From cauldron with MIT License | 4 votes |
def fetch_lexer( source: str, language: str = None, filename: str = None, mime_type: str = None ) -> Lexer: """ :param source: :param language: :param filename: :param mime_type: :return: """ environ.abort_thread() try: if language: return get_lexer_by_name(language, stripall=True) except ClassNotFound: pass if filename: try: return get_lexer_for_filename(filename, stripall=True) except ClassNotFound: pass try: return guess_lexer_for_filename(filename, source, stripall=True) except ClassNotFound: pass try: if mime_type: return get_lexer_for_mimetype(mime_type, stripall=True) except ClassNotFound: pass try: return guess_lexer(source, stripall=True) except ClassNotFound: return TextLexer()
Example #20
Source File: codehilite.py From bazarr with GNU General Public License v3.0 | 4 votes |
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
Example #21
Source File: codehilite.py From bioforum with MIT License | 4 votes |
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
Example #22
Source File: codehilite.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines, wrapcode=True) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
Example #23
Source File: codehilite.py From lambda-packs with MIT License | 4 votes |
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._getLang() if pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = TextLexer() except ValueError: lexer = TextLexer() formatter = HtmlFormatter(linenos=self.linenos, cssclass=self.css_class, style=self.style, noclasses=self.noclasses) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenos: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n'% \ (self.css_class, class_str, txt)
Example #24
Source File: codehilite.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 4 votes |
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)