Pygments

generic syntax highlighter


Ticket #372: pygments-lexer_guess.patch

File pygments-lexer_guess.patch, 1.9 kB (added by JakeWharton, 16 months ago)
  • pygments/cmdline.py

    diff -r 7556dcd33177 pygments/cmdline.py
    a b  
    2929 
    3030       %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>] 
    3131       %s -L [<which> ...] 
     32       %s -N <infile> 
    3233       %s -H <type> <name> 
    3334       %s -h | -V 
    3435 
     
    6768The -L option lists lexers, formatters, styles or filters -- set 
    6869`which` to the thing you want to list (e.g. "styles"), or omit it to 
    6970list everything. 
     71 
     72The -N option attempts to guess the appropriate lexer based solely on 
     73the filename. If no lexer can be determined "text" is returned. 
    7074 
    7175The -H option prints detailed help for the object <name> of type <type>, 
    7276where <type> is one of "lexer", "formatter" or "filter". 
     
    185189    """ 
    186190    # pylint: disable-msg=R0911,R0912,R0915 
    187191 
    188     usage = USAGE % ((args[0],) * 5) 
     192    usage = USAGE % ((args[0],) * 6) 
    189193 
    190194    try: 
    191         popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:hVHg") 
     195        popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHg") 
    192196    except getopt.GetoptError, err: 
    193197        print >>sys.stderr, usage 
    194198        return 2 
     
    260264        else: 
    261265            parsed_opts[name] = value 
    262266    opts.pop('-P', None) 
     267 
     268    # handle ``pygmentize -N`` 
     269    infn = opts.pop('-N', None) 
     270    if infn is not None: 
     271        try: 
     272            lexer = get_lexer_for_filename(infn, **parsed_opts) 
     273        except ClassNotFound, err: 
     274            lexer = TextLexer() 
     275        except OptionError, err: 
     276            print >>sys.stderr, 'Error:', err 
     277            return 1 
     278         
     279        print lexer.aliases[0] 
     280        return 0 
    263281 
    264282    # handle ``pygmentize -S`` 
    265283    S_opt = opts.pop('-S', None)