예제 #1
0
def convert_files(args, source_data: list[str]):
    converted_data = create_paragraph_breaks(source_data)
    if args.source_format == args.format:
        raise Exception('Source and wanted formats are the same')

    elif args.format == StoryFormat.MARKDOWN and args.source_format == StoryFormat.BBCODE:
        logger.info('Converting BBcode to markdown')
        converted_data = [
            convert_BBcode_to_markdown(line) for line in converted_data
        ]
        try:
            validate_markdown(converted_data)
        except ValidationError as e:
            logger.warning(f'Markdown output failed to validate: {e}')
        args.output = pathlib.Path(str(args.output) + '.md')

    elif args.format == StoryFormat.BBCODE and args.source_format == StoryFormat.MARKDOWN:
        logger.info('Converting Markdown to BBcode')
        converted_data = [
            convert_markdown_to_BBcode(line) for line in converted_data
        ]
        try:
            validate_bbCode(converted_data)
        except ValidationError as e:
            logger.warning(f'BBCode output failed to validate: {e}')
        args.output = pathlib.Path(str(args.output) + '.txt')

    else:
        raise Exception(
            f'Unknown format combination: {args.source_format.name} -> {args.format.name}'
        )
    if args.output.exists() and args.overwrite is False:
        raise Exception('Output file exists and overwriting disabled')
    if not args.stdout:
        with open(args.output, 'w') as file:
            file.write(''.join(converted_data))
        logger.info(f'Output file written to {args.output}')
    else:
        for line in converted_data:
            print(line)
def test_complex_strings_markdown_to_BBcode(test_string: str, expected: str):
    result = markdownformatter.convert_markdown_to_BBcode(test_string)
    assert result == expected
def test_link_mixed_markdown_to_BBcode(test_string: str, expected: str):
    result = markdownformatter.convert_markdown_to_BBcode(test_string)
    assert result == expected