Commits

Anonymous committed c97ce1d

#372: add -N option to guess lexer name based on file name.

Comments (0)

Files changed (3)

   because that doesn't keep all newlines at the end when the
   ``stripnl`` lexer option is False.
 
+- Add ``-N`` option to command-line interface to get a lexer name
+  for a given filename.
+
 - Add Tango style, written by Andre Roberge for the Crunchy project.
 
 - Add Python3TracebackLexer and ``python3`` option to 

docs/src/cmdline.txt

 
     $ pygmentize -o test.html test.py
 
+
 Options and filters
 -------------------
 
 the `arg` argument for the formatter's `get_style_defs()` method.
 
 
+Getting lexer names
+-------------------
+
+*New in Pygments 1.0.*
+
+The ``-N`` option guesses a lexer name for a given filename, so that ::
+
+    $ pygmentize -N setup.py
+
+will print out ``python``.  It won't highlight anything yet.  If no specific
+lexer is known for that filename, ``text`` is printed.
+
+
 Getting help
 ------------
 

pygments/cmdline.py

 
        %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>]
        %s -L [<which> ...]
+       %s -N <filename>
        %s -H <type> <name>
        %s -h | -V
 
 `which` to the thing you want to list (e.g. "styles"), or omit it to
 list everything.
 
+The -N option guesses and prints out a lexer name based solely on
+the given filename. It does not take input or highlight anything.
+If no specific lexer can be determined "text" is returned.
+
 The -H option prints detailed help for the object <name> of type <type>,
 where <type> is one of "lexer", "formatter" or "filter".
 
     """
     # pylint: disable-msg=R0911,R0912,R0915
 
-    usage = USAGE % ((args[0],) * 5)
+    usage = USAGE % ((args[0],) * 6)
 
     try:
-        popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:hVHg")
+        popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHg")
     except getopt.GetoptError, err:
         print >>sys.stderr, usage
         return 2
             parsed_opts[name] = value
     opts.pop('-P', None)
 
+    # handle ``pygmentize -N``
+    infn = opts.pop('-N', None)
+    if infn is not None:
+        try:
+            lexer = get_lexer_for_filename(infn, **parsed_opts)
+        except ClassNotFound, err:
+            lexer = TextLexer()
+        except OptionError, err:
+            print >>sys.stderr, 'Error:', err
+            return 1
+
+        print lexer.aliases[0]
+        return 0
+
     # handle ``pygmentize -S``
     S_opt = opts.pop('-S', None)
     a_opt = opts.pop('-a', None)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.