Skip to content

Commit

Permalink
Add procfile lexer (#1808)
Browse files Browse the repository at this point in the history
* add Procfile Lexer

* add unit tests for ProcfileLexer

* add Procfile lexer author

* Document Procfile as supported language

* add Procfile.output file

* merge Profile files; update output file

* Add versionadded info

* Fix typo (processus -> processes)

* detect Integer only

* split Text from Whitespace
  • Loading branch information
sblondon committed Jun 20, 2021
1 parent a537d5e commit 5f976c2
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AUTHORS
Expand Up @@ -32,7 +32,7 @@ Other contributors, listed alphabetically, are:
* Sébastien Bigaret -- QVT Operational lexer
* Jarrett Billingsley -- MiniD lexer
* Adam Blinkinsop -- Haskell, Redcode lexers
* Stéphane Blondon -- SGF and Sieve lexers
* Stéphane Blondon -- Procfile, SGF and Sieve lexers
* Frits van Bommel -- assembler lexers
* Pierre Bourdon -- bugfixes
* Martijn Braam -- Kernel log lexer, BARE lexer
Expand Down
1 change: 1 addition & 0 deletions doc/languages.rst
Expand Up @@ -264,6 +264,7 @@ Other markup
* `OMG IDL <https://www.omg.org/spec/IDL/About-IDL/>`_
* `PEG <https://bford.info/packrat/>`_
* POV-Ray scenes
* `Procfile <https://devcenter.heroku.com/articles/procfile#procfile-format>`_
* `PromQL <https://prometheus.io/docs/prometheus/latest/querying/basics/>`_
* `Puppet <https://puppet.com/>`_
* QML
Expand Down
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -357,6 +357,7 @@
'PowerShellLexer': ('pygments.lexers.shell', 'PowerShell', ('powershell', 'posh', 'ps1', 'psm1'), ('*.ps1', '*.psm1'), ('text/x-powershell',)),
'PowerShellSessionLexer': ('pygments.lexers.shell', 'PowerShell Session', ('ps1con',), (), ()),
'PraatLexer': ('pygments.lexers.praat', 'Praat', ('praat',), ('*.praat', '*.proc', '*.psc'), ()),
'ProcfileLexer': ('pygments.lexers.procfile', 'Procfile', ('procfile',), ('Procfile',), ()),
'PrologLexer': ('pygments.lexers.prolog', 'Prolog', ('prolog',), ('*.ecl', '*.prolog', '*.pro', '*.pl'), ('text/x-prolog',)),
'PromQLLexer': ('pygments.lexers.promql', 'PromQL', ('promql',), ('*.promql',), ()),
'PropertiesLexer': ('pygments.lexers.configs', 'Properties', ('properties', 'jproperties'), ('*.properties',), ('text/x-java-properties',)),
Expand Down
43 changes: 43 additions & 0 deletions pygments/lexers/procfile.py
@@ -0,0 +1,43 @@
"""
pygments.lexers.procfile
~~~~~~~~~~~~~~~~~~~~~~~~
Lexer for Procfile file format.
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, bygroups
from pygments.token import Name, Number, String, Text, Punctuation

__all__ = ["ProcfileLexer"]


class ProcfileLexer(RegexLexer):
"""
Lexer for Procfile file format.
The format is used to run processes on Heroku or is used by Foreman or
Honcho tools.
For more information about the definition of the format, see:
https://devcenter.heroku.com/articles/procfile#procfile-format
.. versionadded:: 2.10
"""
name = 'Procfile'
aliases = ['procfile']
filenames = ['Procfile']

tokens = {
'root': [
(r'^([a-z]+)(:)', bygroups(Name.Label, Punctuation)),
(r'\s+', Text.Whitespace),
(r'"[^"]*"', String),
(r"'[^']*'", String),
(r'[0-9]+', Number.Integer),
(r'\$[a-zA-Z_][\w]*', Name.Variable),
(r'(\w+)(=)(\w+)', bygroups(Name.Variable, Punctuation, String)),
(r'([\w\-\./]+)', Text),
],
}
3 changes: 3 additions & 0 deletions tests/examplefiles/procfile/Procfile
@@ -0,0 +1,3 @@
db: runzeo -C zeoserver.config
web: bundle exec rails server -p $PORT
stuff: env FLASK_ENV=development run -p 9 --stuff 'foobar'
45 changes: 45 additions & 0 deletions tests/examplefiles/procfile/Procfile.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/test_procfile.py
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""
Basic ProcfileLexer Test
~~~~~~~~~~~~~~~~~~~~
:copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

import pytest

from pygments.token import Name, Punctuation, Text
from pygments.lexers.procfile import ProcfileLexer


@pytest.fixture(scope='module')
def lexer():
yield ProcfileLexer()


def test_basic_line(lexer):
text = 'task: executable --options'

tokens = lexer.get_tokens(text)

for index, token in enumerate(tokens):
if index == 0:
assert token == (Name.Label, 'task')
elif index == 1:
assert token == (Punctuation, ':')
else:
assert token[0] in (Text, Text.Whitespace)


def test_environment_variable(lexer):
text = '$XDG_SESSION_PATH'

token = list(lexer.get_tokens(text))[0]

assert token == (Name.Variable, text)

0 comments on commit 5f976c2

Please sign in to comment.