Skip to main content

ReParser

Simple regex-based lexer/parser for inline markup

Requirements

  • Python 3

Usage

Example:

import re
from pprint import pprint
from reparser import Parser, Token, MatchGroup

boundary_chars = r'\s`!()\[\]{{}};:\'".,<>?«»“”‘’*_~='
b_left = r'(?:(?<=[' + boundary_chars + r'])|(?<=^))'  # Lookbehind
b_right = r'(?:(?=[' + boundary_chars + r'])|(?=$))'   # Lookahead

markdown_start = b_left + r'(?<!\\){tag}(?!\s)(?!{tag})'
markdown_end = r'(?<!{tag})(?<!\s)(?<!\\){tag}' + b_right
markdown_link = r'(?<!\\)\[(?P<link>.+?)\]\((?P<url>.+?)\)'
newline = r'\n|\r\n'

url_proto_regex = re.compile(r'(?i)^[a-z][\w-]+:/{1,3}')

def markdown(tag):
    """Return sequence of start and end regex patterns for simple Markdown tag"""
    return (markdown_start.format(tag=tag), markdown_end.format(tag=tag))

def url_complete(url):
    """If URL doesn't start with protocol, prepend it with http://"""
    return url if url_proto_regex.search(url) else 'http://' + url

tokens = [
    Token('bi1',  *markdown(r'\*\*\*'), is_bold=True, is_italic=True),
    Token('bi2',  *markdown(r'___'),    is_bold=True, is_italic=True),
    Token('b1',   *markdown(r'\*\*'),   is_bold=True),
    Token('b2',   *markdown(r'__'),     is_bold=True),
    Token('i1',   *markdown(r'\*'),     is_italic=True),
    Token('i2',   *markdown(r'_'),      is_italic=True),
    Token('pre3', *markdown(r'```'),    skip=True),
    Token('pre2', *markdown(r'``'),     skip=True),
    Token('pre1', *markdown(r'`'),      skip=True),
    Token('s',    *markdown(r'~~'),     is_strikethrough=True),
    Token('u',    *markdown(r'=='),     is_underline=True),
    Token('link', markdown_link, text=MatchGroup('link'),
          link_target=MatchGroup('url', func=url_complete)),
    Token('br', newline, text='\n', segment_type="LINE_BREAK")
]

parser = Parser(tokens)
text = ('Hello **bold** world!\n'
        'You can **try *this* awesome** [link](www.eff.org).')

segments = parser.parse(text)
pprint([(segment.text, segment.params) for segment in segments])

Output:

[('Hello ', {}),
 ('bold', {'is_bold': True}),
 (' world!', {}),
 ('\n', {'segment_type': 'LINE_BREAK'}),
 ('You can ', {}),
 ('try ', {'is_bold': True}),
 ('this', {'is_bold': True, 'is_italic': True}),
 (' awesome', {'is_bold': True}),
 (' ', {}),
 ('link', {'link_target': 'http://www.eff.org'}),
 ('.', {})]

Metadata

Release files for ReParser 1.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ReParser 1.4.3
File Size Uploaded Provenance
ReParser-1.4.3.tar.gz 4.6 kB Without provenance Details

Release files / ReParser-1.4.3.tar.gz

Download URL ReParser-1.4.3.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e69caf58a29d6e04723f6a7a456d304b7acfcf413957dafcd90ee49eccc2d15a
BLAKE2b-256 checksum
How to use checksums
a7eb8bd06ddaeb3d5963abb0e2e2f14f2fa975b32fafe198254bfb4106ed9415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for ReParser 1.4.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page