{"id":17093408,"url":"https://github.com/crowsonkb/pyparsing-highlighting","last_synced_at":"2025-04-12T22:44:07.926Z","repository":{"id":62582410,"uuid":"164399303","full_name":"crowsonkb/pyparsing-highlighting","owner":"crowsonkb","description":"Syntax highlighting for prompt_toolkit and HTML with pyparsing.","archived":false,"fork":false,"pushed_at":"2019-06-02T19:56:50.000Z","size":131,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T22:43:35.296Z","etag":null,"topics":["prompt-toolkit","pyparsing","python-library","syntax-highlighting"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crowsonkb.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-07T08:11:30.000Z","updated_at":"2023-02-24T15:05:49.000Z","dependencies_parsed_at":"2022-11-03T22:01:40.665Z","dependency_job_id":null,"html_url":"https://github.com/crowsonkb/pyparsing-highlighting","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowsonkb%2Fpyparsing-highlighting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowsonkb%2Fpyparsing-highlighting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowsonkb%2Fpyparsing-highlighting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crowsonkb%2Fpyparsing-highlighting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crowsonkb","download_url":"https://codeload.github.com/crowsonkb/pyparsing-highlighting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643047,"owners_count":21138353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["prompt-toolkit","pyparsing","python-library","syntax-highlighting"],"created_at":"2024-10-14T14:06:52.982Z","updated_at":"2025-04-12T22:44:07.902Z","avatar_url":"https://github.com/crowsonkb.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"pyparsing-highlighting\n======================\n\nSyntax highlighting with `pyparsing \u003chttps://github.com/pyparsing/pyparsing\u003e`_, supporting both HTML output and `prompt_toolkit \u003chttps://github.com/prompt-toolkit/python-prompt-toolkit\u003e`_–style terminal output. The ``PPHighlighter`` class can also be used as a lexer for syntax highlighting as you type in prompt_toolkit. It is compatible with existing `Pygments \u003chttp://pygments.org\u003e`_ styles.\n\nThe main benefit of pyparsing-highlighting over Pygments is that pyparsing parse expressions are both more powerful and easier to understand than Pygments lexers. pyparsing implements `parsing expression grammars \u003chttps://en.wikipedia.org/wiki/Parsing_expression_grammar\u003e`_ using `parser combinators \u003chttps://en.wikipedia.org/wiki/Parser_combinator\u003e`_, which means that higher level parse expressions are built up in Python code out of lower level parse expressions in a straightforward to construct, readable, modular, well-structured, and easily maintainable way.\n\nSee `the official pyparsing documentation \u003chttps://pyparsing-docs.readthedocs.io/en/latest/index.html\u003e`_ or `my unofficial (epydoc) documentation \u003chttps://pyparsing-doc.neocities.org\u003e`_; read the pyparsing-highlighting documentation on `readthedocs \u003chttps://pyparsing-highlighting.readthedocs.io/en/latest/\u003e`_.\n\nRequirements\n------------\n\n- `Python \u003chttps://www.python.org\u003e`_ 3.5+\n\nNote that `PyPy \u003chttps://pypy.org\u003e`_, a JIT compiler implementation of Python, is often able to achieve around 5x the performance of CPython, the reference Python implementation.\n\n- `pyparsing \u003chttps://github.com/pyparsing/pyparsing\u003e`_\n- `prompt_toolkit \u003chttps://github.com/prompt-toolkit/python-prompt-toolkit\u003e`_ 2.0+\n- `Pygments \u003chttp://pygments.org\u003e`_ (optional; needed to use Pygments styles)\n\nInstallation\n------------\n\n.. code:: bash\n\n   pip3 install -U pyparsing-highlighting\n\nOr, after cloning the repository on GitHub:\n\n.. code:: bash\n\n   python3 setup.py install\n\n(or, with PyPy):\n\n.. code:: bash\n\n   pypy3 setup.py install\n\nExamples\n--------\n\nThe following code demonstrates the use of ``PPHighlighter``:\n\n.. code:: python\n\n   from pp_highlighting import PPHighlighter\n   from prompt_toolkit.styles import Style\n   import pyparsing as pp\n   from pyparsing import pyparsing_common as ppc\n\n   def parser_factory(styler):\n       a = styler('class:int', ppc.integer)\n       return pp.delimitedList(a)\n\n   pph = PPHighlighter(parser_factory)\n   style = Style([('int', '#528f50')])\n   pph.print('1, 2, 3', style=style)\n\nThis prints out the following to the terminal:\n\n.. image:: https://raw.githubusercontent.com/crowsonkb/pyparsing-highlighting/master/docs/source/example_ints.png\n   :width: 56\n   :height: 18\n   :alt: 1, 2, 3\n\nThe following code generates HTML:\n\n.. code:: python\n\n   pph.highlight_html('1, 2, 3')\n\nThe output is:\n\n.. code:: HTML\n\n   \u003cpre class=\"highlight\"\u003e\u003cspan class=\"int\"\u003e1\u003c/span\u003e, \u003cspan class=\"int\"\u003e2\u003c/span\u003e, \u003cspan class=\"int\"\u003e3\u003c/span\u003e\u003c/pre\u003e\n\nThere is also a lower-level API—:code:`pph.highlight('1, 2, 3')` returns the following::\n\n   FormattedText([('class:int', '1'), ('', ', '), ('class:int', '2'), ('', ', '), ('class:int', '3')])\n\nA ``FormattedText`` instance can be passed to ``prompt_toolkit.print_formatted_text()``, along with a ``Style`` mapping the class names to colors, for display on the terminal. See the prompt_toolkit `formatted text documentation \u003chttps://python-prompt-toolkit.readthedocs.io/en/stable/pages/printing_text.html#style-text-tuples\u003e`_ and `formatted text API documentation \u003chttps://python-prompt-toolkit.readthedocs.io/en/stable/pages/reference.html#module-prompt_toolkit.formatted_text\u003e`_.\n\n``PPHighlighter`` can also be passed to a ``prompt_toolkit.PromptSession`` as the ``lexer`` argument, which will perform syntax highlighting as you type. For examples of this, see ``examples/calc.py``, ``examples/json_pph.py``, ``examples/repr.py``, and ``examples/sexp.py``. The examples can be run by (from the project root directory):\n\n.. code:: bash\n\n   python3 -m examples.calc\n   python3 -m examples.json_pph\n   python3 -m examples.repr\n   python3 -m examples.sexp\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrowsonkb%2Fpyparsing-highlighting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrowsonkb%2Fpyparsing-highlighting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrowsonkb%2Fpyparsing-highlighting/lists"}