Skip to content

Advanced replacement

mkdocs.yml

# This example shows how to replace lines with regex expressions!
site_name: ok-source-replace
plugins:
  - simple:
      semiliterate:
        - pattern: \.py$
          extract:
            - start: ^\s*"""\W?md\b
              stop: ^\s*"""\s*$
              replace: 
                # replace "foo" with "bar"
                - ["(.*)foo(.*)$", "\\1bar\\2"] 
                # only capture what's after args and replace with parameter prefix
                - ["args:(.*)$","parameters:\\1"] 
                # capture everything after `only_this:`
                - "only_this:(.*)$"
                # drop lines starting with drop 
                - "^drop"

Input

ok-source-replace/
├── mkdocs.yml
├── module.py
└── README.md

module.py

"""md

## Python Version

You can put _markdown_ in triple-quoted strings in Python.

You can even replace things!

You should see foo replaced by bar in this sentence.

args: This line should start with `parameters`.

blah blah blah only_this: Only this is captured.

drop this line!
"""


def main():
    """Main test."""
    print("Hello, world!")
    return 0

Output

site/
├── index.html
└── module/
    └── index.html

module

Python Version

You can put markdown in triple-quoted strings in Python.

You can even replace things!

You should see bar replaced by bar in this sentence.

parameters: This line should start with parameters.

Only this is captured.