iamjackg / md2cf

Convert and upload Markdown documents to Confluence
MIT License
93 stars 52 forks source link

Added replacement facility for page content. #104

Open jhogstrom opened 1 year ago

jhogstrom commented 1 year ago

By using either command line replacement pairs or specifying a file with json defintions of replacements an arbitrary regexp ("pattern") can be detected and replaced with another string, including expanding captured groups in the pattern.

The replacement phase is taking place just before upsert, so all other textual manipulations are done by that time.

Replacements happen in a deterministic sequence. There are ample opportunities to get unexpected (but logically consistent) results by inadvertently result of a previous replacement.

Format of json file:

{
    "environment": [
        {
            "import": "<module name>",
            "path": "<source file>"
        }
    ],
    "replacements":[
        {
            "name": "<name - optional>",
            "pattern": "<regexp>",
            "new_value": "<string with optional group expansions>"
            "evaluate": <true|false - optional>
        },
    ]
}

The environment block is optional and used for very dynamic replacements. By specifying a python source file, it will be dynamically imported at run time. The new_value field can then specify a <module>.<func> that returns a string value. As an example, the following adds a replacement of "TODAY" to an iso-formatted datetime.

{
    "environment": [
        {
            "import": "funcs",
            "path": "funcs.py"
        }
    ],
    "replacements":[
        {
            "name": "Todays date",
            "pattern": "TODAY",
            "new_value": "funcs.today"
            "evaluate": true
        },
    ]
}

Funcs.py:

import datetime

def today(term):
    return datetime.datetime.now().isoformat()

The parameter term is a Match object as per using https://docs.python.org/3/library/re.html#re.subn.