|
| 1 | +# Markdown to DW deployment script |
| 2 | +# inspired by https://github.com/cvut/MI-PYT |
| 3 | +import os |
| 4 | +import re |
| 5 | + |
| 6 | +import click |
| 7 | +import pypandoc |
| 8 | +import requests |
| 9 | + |
| 10 | +from lxml import etree |
| 11 | + |
| 12 | + |
| 13 | +def prefilter_haskellcode(input_md): |
| 14 | + """Make use of pascal to enable haskell code highlighting (in md)""" |
| 15 | + return input_md.replace('```haskell', '```pascal') |
| 16 | + |
| 17 | + |
| 18 | +def postfilter_haskellcode(output_dw): |
| 19 | + """Make use of pascal to enable haskell code highlighting (in dw)""" |
| 20 | + return output_dw.replace('<code pascal>', '<code haskell>') |
| 21 | + |
| 22 | + |
| 23 | +def prefilter_interlinking(input_md): |
| 24 | + """Removed md from local interlinked documents in the same directory""" |
| 25 | + return re.sub(r'\[([^\]]*)]\(([^\)\/]*)\.md\)', r'[\1](\2)', input_md) |
| 26 | + |
| 27 | + |
| 28 | +class DW: |
| 29 | + PREFILTERS = [ |
| 30 | + prefilter_haskellcode, |
| 31 | + prefilter_interlinking |
| 32 | + ] |
| 33 | + POSTFILTERS = [ |
| 34 | + postfilter_haskellcode |
| 35 | + ] |
| 36 | + |
| 37 | + def __init__(self, url, username, password): |
| 38 | + self.url = url + '/doku.php' |
| 39 | + self.session = self._init_session(username, password) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def _find_elem(content, type, attr, value): |
| 43 | + tree = etree.HTML(content) |
| 44 | + for elem in tree.findall('.//' + type): |
| 45 | + if elem.attrib.get(attr) == value: |
| 46 | + return elem |
| 47 | + return None |
| 48 | + |
| 49 | + def _init_session(self, username, password): |
| 50 | + session = requests.Session() |
| 51 | + |
| 52 | + params = {'id': 'start', 'do': 'login'} |
| 53 | + r = session.get(self.url, params=params) |
| 54 | + sectok_input = self._find_elem(r.text, 'input', 'name', 'sectok') |
| 55 | + if sectok_input is None: |
| 56 | + raise ValueError('Could not find sectok on login page') |
| 57 | + |
| 58 | + sectok = sectok_input.attrib.get('value', '') |
| 59 | + login_data = { |
| 60 | + 'sectok': sectok, |
| 61 | + 'id': 'start', |
| 62 | + 'do': 'login', |
| 63 | + 'authnProvider': '2', |
| 64 | + 'u': username, |
| 65 | + 'p': password, |
| 66 | + 'r': '1', |
| 67 | + } |
| 68 | + params['sectok'] = sectok |
| 69 | + r = session.post(self.url, params=params, data=login_data) |
| 70 | + if 'logout' not in r.text: |
| 71 | + raise ValueError('Could not login') |
| 72 | + |
| 73 | + return session |
| 74 | + |
| 75 | + def put_page(self, dw_page, content): |
| 76 | + params = {'id': dw_page, 'do': 'edit'} |
| 77 | + r = self.session.get(self.url, params=params) |
| 78 | + |
| 79 | + edit_form = self._find_elem(r.text, 'form', 'id', 'dw__editform') |
| 80 | + if edit_form is None: |
| 81 | + raise ValueError('Could not find edit form on parsed page') |
| 82 | + |
| 83 | + data = {} |
| 84 | + for inp in edit_form.findall('.//input'): |
| 85 | + name = inp.attrib.get('name') |
| 86 | + if not name.startswith('do['): |
| 87 | + data[name] = inp.attrib.get('value', '') |
| 88 | + |
| 89 | + data['wikitext'] = content |
| 90 | + data['do[save]'] = 'Yes, please!' |
| 91 | + |
| 92 | + self.session.post(r.url, data=data) |
| 93 | + |
| 94 | + def put_md(self, dw_page, file): |
| 95 | + self.put_page(dw_page, self._transform_md2dw(file)) |
| 96 | + |
| 97 | + @classmethod |
| 98 | + def _transform_md2dw(cls, file): |
| 99 | + with open(file, mode='r') as f: |
| 100 | + content = f.read() |
| 101 | + content = cls._apply_filters(cls.PREFILTERS, content) |
| 102 | + content = pypandoc.convert_text(content, 'dokuwiki', format='md') |
| 103 | + content = cls._apply_filters(cls.POSTFILTERS, content) |
| 104 | + return content |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def _apply_filters(filters, text): |
| 108 | + for flt in filters: |
| 109 | + text = flt(text) |
| 110 | + return text |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def join(*args): |
| 114 | + return ':'.join(args) |
| 115 | + |
| 116 | + |
| 117 | +def get_files(root, extension): |
| 118 | + files = dict() |
| 119 | + ext_mlen = -len(extension) |
| 120 | + for file in os.listdir(root): |
| 121 | + path = os.path.join(root, file) |
| 122 | + if os.path.isfile(path) and file.endswith(extension): |
| 123 | + files[file[0:ext_mlen]] = path |
| 124 | + return files |
| 125 | + |
| 126 | + |
| 127 | +@click.command() |
| 128 | +@click.argument('root', type=click.Path(exists=True)) |
| 129 | +@click.option('-d', '--dw-url', help='DokuWiki URL', |
| 130 | + envvar='DW_URL', required=True) |
| 131 | +@click.option('-u', '--dw-username', help='DokuWiki username', |
| 132 | + envvar='DW_USERNAME', required=True) |
| 133 | +@click.option('-p', '--dw-password', help='DokuWiki password', |
| 134 | + envvar='DW_PASSWORD', required=True) |
| 135 | +@click.option('-n', '--dw-namespace', help='Target DokuWiki namespace', |
| 136 | + envvar='DW_NAMESPACE', required=True) |
| 137 | +def cli(root, dw_url, dw_username, dw_password, dw_namespace): |
| 138 | + dw = DW(dw_url, dw_username, dw_password) |
| 139 | + markdowns = get_files(root, '.md') |
| 140 | + print('Deploying {} markdown file(s) to {}'.format( |
| 141 | + len(markdowns), dw_url |
| 142 | + )) |
| 143 | + for name, file in markdowns.items(): |
| 144 | + qname = dw.join(dw_namespace, name) |
| 145 | + print('| ', file, '-->', qname) |
| 146 | + dw.put_md(qname, file) |
| 147 | + print('Done!') |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + cli() |
0 commit comments