Skip to main content

Markup & Source Code 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.3.tar.gz (7.4 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.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: muppy-1.0.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for muppy-1.0.3.tar.gz
Algorithm Hash digest
SHA256 037c1aa0fb4f47aa297b7868654112e04c06f51a616a2cf52cc1e9cdb542f325
MD5 73036e7b3f8bcbd16b67172b6d9c436e
BLAKE2b-256 0ca02f2545813aec1ee42cd93818131003c6bd6065098641868ce2585031d092

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for muppy-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f3c5378879479d531d4a125bcc8413878dc4f10af8f0275d673a641f3cdb2e17
MD5 6a92c244d0e10a80b6eaa0e9ae4775ac
BLAKE2b-256 d9bab6896ede75605f2acc00846e62386c4eb4ef61f00cb817fd1026d6737ea7

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