Skip to main content

Markup Preprocessor for Python

Project description

Logo

Description

PyPI - Version PyPI - Python Version PyPI - Status PyPI - Downloads PyPI - License Gitea Issues Gitea Last Commit

Muppy means MarkUp Preprocessor for Python. If you want some Python in markup, not some markup in Python—Muppy is probably the thing you need.


WARNING: This script is using exec(). Be careful when you execute Muppy with a file you get from strangers, and keep in mind that it can be harmful as any other Python script. Use Muppy script standalone from the git repo if you have reasons not to trust the wheel.

Installation

The script is pure Python and a part of PyPI, so can be installed via pip:

python3 -m pip install muppy

So, what does it do, exactly?

Technically, Muppy gets comments in various formats and executes them as Python code. Any text between comment blocks is treated as string literals.

Basic HTML example

Contents of test.html. Mind Python indentation after <!-- (py):, placeholders ?????, and empty lines <!-- (py):-->:

<!-- (py):
result = ?????-->
<html>
 <head>
  <title>
   <!-- (py):if lang == 'en':--><!-- (py): result += ?????-->Hello world!<!-- (py):-->
   <!-- (py):if lang == 'ru':--><!-- (py): result += ?????-->Привет, мир!<!-- (py):-->
<!-- (py):result += ?????-->
  </title>
 </head>
 <body>
 </body>
</html>
<!-- (py):print(result)-->

Shell command:

muppy compile -s xml -i test.html -d "lang = 'en'"

The preprocessor code to be executed:

# string literals
__muppy0 = ''
__muppy1 = '\n<html>\n <head>\n  <title>\n   '
__muppy2 = ''
__muppy3 = 'Hello world!'
__muppy4 = '\n   '
__muppy5 = ''
__muppy6 = 'Привет, мир!'
__muppy7 = '\n'
__muppy8 = '\n  </title>\n </head>\n <body>\n </body>\n</html>\n'
__muppy9 = '\n'

# definitions
lang = 'en'

# code
result = __muppy1
if lang == 'en':
 result += __muppy3

if lang == 'ru':
 result += __muppy6

result += __muppy8
print(result)

Result:

<html>
 <head>
  <title>
   Hello world!
  </title>
 </head>
 <body>
 </body>
</html>

Comment styles

For now, Muppy supports the following comment styles (-s):

Style Start tag End tag Description
xml <!-- (py): --> Any XML format: HTML, SVG, FB2, etc.
c /* (py): */ C, C++, CSS, PHP, etc.
tex % (py): Newline TeX-compatible markup
shell # (py): Newline Bash, Python, etc.

Definitions

You can define (-d) any variables as you do in common Python code:

muppy compile -s tex -i test.html -d 'var1 = 10' 'some_list = ["a", "b", "c"]'

Bugs

Feel free to report bugs and request features here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muppy-1.0.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

muppy-1.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file muppy-1.0.2.tar.gz.

File metadata

  • Download URL: muppy-1.0.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for muppy-1.0.2.tar.gz
Algorithm Hash digest
SHA256 60b4e0baeb5918807f02adece9bc3e0de998c7b53cc60048cddb4b1848423176
MD5 2fb5bd7a0071d0d91572d184b56b535b
BLAKE2b-256 9d721c1bcd99f378d132de3159fff769e1e014608e2c7f8cab6af75049babdde

See more details on using hashes here.

File details

Details for the file muppy-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: muppy-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for muppy-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10be376f3c0283d09f0a85c815b9ec733a55c26c9045dd134efc00579c52652c
MD5 34a4606b10a6281b31ca4ff0698bde3d
BLAKE2b-256 c9fd8d28fe0f3d42fe37dec08ece9ce01b61eb692a30c0df4dedd988f27f78c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page