Attached are two patches adding highlighting support for Makefiles. Files named 'makefile', 'Makefile' or 'GNUmakefile', as well as files whose firstline is a make shebang, will be highlighted as Makefiles.
I refrained from adding "Closes: #782109" in the second commit's message because 'debian/rules' files without a shebang won't be detected as Makefiles: only the filename, not the entire path, is passed to filetype.get_filetype(). I'm not sure if that's a problem.
>From 7cbcd7dfd121e0c37e5d71897f9b9cc77ef2a185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Schreiner?= <clem...@mux.me> Date: Wed, 8 Apr 2015 20:56:36 +0200 Subject: [PATCH 1/2] filetype: fix shebang matching when arguments are passed to the interpreter Rationale: #!/usr/bin/make -f didn't match like #!/usr/bin/make --- debsources/filetype.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/debsources/filetype.py b/debsources/filetype.py index 5fca6fe..8030e0d 100644 --- a/debsources/filetype.py +++ b/debsources/filetype.py @@ -172,7 +172,8 @@ def get_filetype_from_firstline(firstline): if interp.startswith("env"): # shebang #!/usr/bin/env foo interp = interp.split()[-1] else: # shebang #!/usr/bin/foo - pass + # ignore options passed to the interpreter + interp = interp.split()[0] if interp in shebangs.keys(): return shebangs[interp] else: -- 2.1.4
>From 1e8695590b4df174deefb4e97c9647decaf1eed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Schreiner?= <clem...@mux.me> Date: Wed, 8 Apr 2015 21:03:29 +0200 Subject: [PATCH 2/2] filetype: add support for Makefile files Changes: - matching makefile, Makefile and GNUmakefile filenames to a new filetype, MAKEFILE - the shebang 'make' now maps to MAKEFILE (formerly CMAKE) - highlightjs: map MAKEFILE to 'makefile' Unit tests have been added for the above modifications. --- debsources/filetype.py | 9 ++++++--- debsources/tests/test_filetype.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/debsources/filetype.py b/debsources/filetype.py index 8030e0d..a2d756b 100644 --- a/debsources/filetype.py +++ b/debsources/filetype.py @@ -20,8 +20,9 @@ from six.moves import range (PYTHON, RUBY, PERL, PHP, SCALA, GO, XML, HTML, MARKDOWN, CSS, JSON, JAVASCRIPT, COFFEESCRIPT, ACTIONSCRIPT, VBSCRIPT, LUA, JAVA, C, CPP, OBJECTIVEC, VALA, CSHARP, D, SQL, LISP, CLOJURE, INI, APACHE, CMAKE, VHDL, - DIFF, BASH, TEX, BRAINFUCK, HASKELL, ERLANG, RUST, R, OCAML, SCILAB) \ - = list(range(40)) + DIFF, BASH, TEX, BRAINFUCK, HASKELL, ERLANG, RUST, R, OCAML, SCILAB, + MAKEFILE) \ + = list(range(41)) # Languages strings used by highlight.js highlightjs = { @@ -65,6 +66,7 @@ highlightjs = { R: "r", OCAML: "ocaml", SCILAB: "scilab", + MAKEFILE: "makefile", } # Filename regexes @@ -113,6 +115,7 @@ filename_regexes = [ (R, [r'\.r$', r'\.R$']), (OCAML, [r'\.ml$', r'\.mli$']), (SCILAB, [r'\.sci$', r'\.sce$']), + (MAKEFILE, [r'^(GNUm|m|M)akefile$']), ] # Shebang map: @@ -126,7 +129,7 @@ shebangs = dict( php=PHP, ruby=RUBY, # tcl = TCL, - make=CMAKE, + make=MAKEFILE, zsh=BASH, ksh=BASH, csh=BASH, diff --git a/debsources/tests/test_filetype.py b/debsources/tests/test_filetype.py index 07d4e2b..a4bfaca 100644 --- a/debsources/tests/test_filetype.py +++ b/debsources/tests/test_filetype.py @@ -17,7 +17,7 @@ from nose.tools import istest from nose.plugins.attrib import attr from debsources.filetype import get_filetype, get_highlightjs_language -from debsources.filetype import HTML, PHP, PYTHON, RUBY, XML +from debsources.filetype import HTML, PHP, PYTHON, RUBY, XML, MAKEFILE @attr('filetype') @@ -75,3 +75,32 @@ class FiletypeTests(unittest.TestCase): "#!/bin/perl\n", None), "perl") + + @istest + def makefileFilename(self): + self.assertEqual(get_filetype('Makefile', 'foobar'), MAKEFILE) + + @istest + def makefileFilenameLowerCase(self): + self.assertEqual(get_filetype('makefile', 'foobar'), MAKEFILE) + + @istest + def assertAutomakeNotMakefile(self): + self.assertNotEqual(get_filetype('Makefile.am', 'foobar'), MAKEFILE) + + @istest + def makefileShebang(self): + self.assertEqual(get_filetype('foo', '#!/usr/bin/make -f'), MAKEFILE) + + @istest + def hilightjsLanguageMakefile(self): + self.assertEqual(get_highlightjs_language("Makefile", "foobar", None), + "makefile") + + @istest + def hilightjsLanguageMakeShebang(self): + self.assertEqual(get_highlightjs_language("foo", + "#!/usr/bin/make -f", + None), + "makefile") + -- 2.1.4