Commits

Anonymous committed 59849a1

Add sqlite3 console lexer from #351

  • Participants
  • Parent commits 10cb325
  • Branches trunk

Comments (0)

Files changed (3)

   * Gnuplot plotting scripts
   * Clojure
   * POV-Ray scene files
+  * Sqlite3 interactive console sessions
 
 - Lexers improved:
   * C lexer highlights standard library functions now and supports

File pygments/lexers/_mapping.py

     'SmartyLexer': ('pygments.lexers.templates', 'Smarty', ('smarty',), ('*.tpl',), ('application/x-smarty',)),
     'SourcesListLexer': ('pygments.lexers.text', 'Debian Sourcelist', ('sourceslist', 'sources.list'), ('sources.list',), ()),
     'SqlLexer': ('pygments.lexers.other', 'SQL', ('sql',), ('*.sql',), ('text/x-sql',)),
+    'SqliteConsoleLexer': ('pygments.lexers.other', 'sqlite3con', (), ('*.sqlite3-console',), ('text/x-sqlite3-console',)),
     'SquidConfLexer': ('pygments.lexers.text', 'SquidConf', ('squidconf', 'squid.conf', 'squid'), ('squid.conf',), ('text/x-squidconf',)),
     'TclLexer': ('pygments.lexers.agile', 'Tcl', ('tcl',), ('*.tcl',), ('text/x-tcl', 'text/x-script.tcl', 'application/x-tcl')),
     'TcshLexer': ('pygments.lexers.other', 'Tcsh', ('tcsh', 'csh'), ('*.tcsh', '*.csh'), ('application/x-csh',)),

File pygments/lexers/other.py

 
 import re
 
-from pygments.lexer import RegexLexer, include, bygroups, using, this
+from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, this, \
+                           do_insertions
 from pygments.token import Error, Punctuation, \
-     Text, Comment, Operator, Keyword, Name, String, Number
+     Text, Comment, Operator, Keyword, Name, String, Number, Generic
 from pygments.util import shebang_matches
 
 
-__all__ = ['SqlLexer', 'MySqlLexer', 'BrainfuckLexer', 'BashLexer',
-           'BatchLexer', 'BefungeLexer', 'RedcodeLexer', 'MOOCodeLexer',
-           'SmalltalkLexer', 'TcshLexer', 'LogtalkLexer', 'GnuplotLexer',
-           'PovrayLexer']
+__all__ = ['SqlLexer', 'MySqlLexer', 'SqliteConsoleLexer', 'BrainfuckLexer',
+           'BashLexer', 'BatchLexer', 'BefungeLexer', 'RedcodeLexer',
+           'MOOCodeLexer', 'SmalltalkLexer', 'TcshLexer', 'LogtalkLexer',
+           'GnuplotLexer', 'PovrayLexer']
+
+line_re  = re.compile('.*?\n')
 
 
 class SqlLexer(RegexLexer):
     }
 
 
+class SqliteConsoleLexer(Lexer):
+    """
+    Lexer for example sessions using sqlite3.
+    """
+
+    name = 'sqlite3con'
+    aliases = []
+    filenames = ['*.sqlite3-console']
+    mimetypes = ['text/x-sqlite3-console']
+
+    def get_tokens_unprocessed(self, data):
+        sql = SqlLexer(**self.options)
+
+        curcode = ''
+        insertions = []
+        for match in line_re.finditer(data):
+            line = match.group()
+            if line.startswith('sqlite> ') or line.startswith('   ...> '):
+                insertions.append((len(curcode),
+                                   [(0, Generic.Prompt, line[:8])]))
+                curcode += line[8:]
+            else:
+                if curcode:
+                    for item in do_insertions(insertions,
+                                              sql.get_tokens_unprocessed(curcode)):
+                        yield item
+                    curcode = ''
+                    insertions = []
+                if line.startswith('SQL error: '):
+                    yield (match.start(), Generic.Traceback, line)
+                else:
+                    yield (match.start(), Generic.Output, line)
+        if curcode:
+            for item in do_insertions(insertions,
+                                      sql.get_tokens_unprocessed(curcode)):
+                yield item
+
+
 class BrainfuckLexer(RegexLexer):
     """
     Lexer for the esoteric `BrainFuck <http://www.muppetlabs.com/~breadbox/bf/>`_