Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # md2dk.py
- # Created by dturba at 13/03/2020
- import re
- from argparse import ArgumentParser, Namespace
- from typing import List, Optional
- _link_pattern = re.compile(r'\[(?P<name>[^\]]+)(\])(\()(?P<link>[^)]+)\)',
- flags=0)
- def title_formatting(*, line: str, **_) -> str:
- if line.startswith('#' * 6):
- line = f"{line.replace('#' * 6, '=')} ="
- elif line.startswith('#' * 5):
- line = f"{line.replace('#' * 5, '=' * 2)} {'=' * 2}"
- elif line.startswith('#' * 4):
- line = f"{line.replace('#' * 4, '=' * 3)} {'=' * 3}"
- elif line.startswith('#' * 3):
- line = f"{line.replace('#' * 3, '=' * 4)} {'=' * 4}"
- elif line.startswith('#' * 2):
- line = f"{line.replace('#' * 2, '=' * 5)} {'=' * 5}"
- elif line.startswith('#'):
- line = f"{line.replace('#', '=' * 6)} {'=' * 6}"
- return line
- def code_block_formatting(
- *, line: str, lines: List[str], current_i: int, **_) -> str:
- if '```' in line:
- line = line.replace('```', '')
- for i, tmp_line in enumerate(lines[current_i + 1:], 1):
- if '```' in tmp_line:
- lines[current_i + i] = lines[current_i + i].replace('```', '')
- break
- else:
- lines[current_i + i] = ''.join((' ', lines[current_i + i]))
- return line
- def list_formatting(*, line: str, **_) -> str:
- if line.startswith('- '):
- line = line.replace('- ', ' * ')
- return line
- def monospace_formatting(*, line: str, **_) -> str:
- new_line = line
- while new_line.find('`') != -1:
- new_line = new_line.replace('`', "''%%", 1)
- new_line = new_line.replace('`', "%%''", 1)
- return new_line
- def link_formatting(*, line: str, **_) -> str:
- new_line = line
- link_match = _link_pattern.search(new_line)
- while link_match:
- new_line = f"{new_line[:link_match.start()]}[[" \
- f"{link_match.group('link')}|" \
- f"{link_match.group('name')}]]" \
- f"{new_line[link_match.end():]}"
- link_match = _link_pattern.search(new_line)
- return new_line
- def formatting(text: str) -> str:
- lines = text.splitlines()
- format_function_li = [
- title_formatting,
- code_block_formatting,
- list_formatting,
- monospace_formatting,
- link_formatting,
- ]
- final_lines = list()
- for i, line in enumerate(lines):
- for format_function in format_function_li:
- line = format_function(line=line, lines=lines, current_i=i)
- final_lines.append(line)
- final_text = '\n'.join(final_lines)
- return final_text
- def convert_md_to_mw(input_filename: str, output_filename: Optional[str],
- open_mode: str) -> None:
- with open(input_filename, 'r') as input_file:
- final_text = formatting(input_file.read())
- if not output_filename:
- return print(final_text)
- with open(output_filename, open_mode) as output_file:
- output_file.write(final_text)
- return print(f'{output_filename} written')
- if __name__ == '__main__':
- parser = ArgumentParser(prog='md2dk',
- usage='%(prog)s [-h] [-o O] input_file [--w]',
- description='Convert a file from markdown to '
- 'dokuwiki. If no output file is given,'
- ' the standard output will be used.')
- parser.add_argument('input_file', type=str,
- help='the file you need to convert')
- parser.add_argument('-o',
- help='name of output file in dokuwiki format')
- parser.add_argument('--w', default='x', const='w', action='store_const',
- help='if you want to overwrite the output file')
- args: Namespace = parser.parse_args()
- convert_md_to_mw(args.input_file, args.o, args.w)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement