├── editor_with_functions.py └── editor.py /editor_with_functions.py: -------------------------------------------------------------------------------- 1 | def get_text(): 2 | return input("Text: ") 3 | 4 | 5 | def plain(): 6 | return get_text() 7 | 8 | 9 | def bold(): 10 | return f"**{get_text()}**" 11 | 12 | 13 | def italic(): 14 | return f"*{get_text()}*" 15 | 16 | 17 | def link(): 18 | label = input("Label: ") 19 | url = input("URL: ") 20 | 21 | return f"[{label}]({url})" 22 | 23 | 24 | def inline_code(): 25 | return f"`{get_text()}`" 26 | 27 | 28 | def new_line(): 29 | return "\n" 30 | 31 | 32 | def header(): 33 | while True: 34 | lvl = input("Level: ") 35 | if len(lvl) == 1 and lvl in "123456": 36 | break 37 | else: 38 | print("The level should be within the range of 1 to 6") 39 | 40 | return f"{'#' * int(lvl)} {get_text()}\n" 41 | 42 | 43 | def unordered_list(): 44 | return '\n'.join(list(map(lambda line: '*' + line[2:], general_list()))) + '\n' 45 | 46 | 47 | def ordered_list(): 48 | return '\n'.join(general_list()) + '\n' 49 | 50 | 51 | def general_list(): 52 | while True: 53 | n = int(input("Number of rows: ")) 54 | if n > 0: 55 | break 56 | else: 57 | print("The number of rows should be greater than zero") 58 | return [f"{i}. {input(f'Row #{i}: ')}" for i in range(1, n + 1)] 59 | 60 | 61 | def save_to_file(): 62 | with open("output.md", 'w') as f: 63 | f.write(''.join(result)) 64 | 65 | 66 | formatters = {"plain": plain, 67 | "bold": bold, 68 | "italic": italic, 69 | "link": link, 70 | "inline-code": inline_code, 71 | "new-line": new_line, 72 | "header": header, 73 | "ordered-list": ordered_list, 74 | "unordered-list": unordered_list, 75 | } 76 | 77 | result = [] 78 | while True: 79 | formatter = input("Choose a formatter: ") 80 | if formatter == "!done": 81 | save_to_file() 82 | break 83 | elif formatter == "!help": 84 | print(f"Available formatters: {' '.join(formatters.keys())}\nSpecial commands: !help !done") 85 | elif formatter not in formatters: 86 | print("Unknown formatter or command. Please try again.") 87 | else: 88 | result.append(formatters[formatter]()) 89 | print(*result, sep='') 90 | -------------------------------------------------------------------------------- /editor.py: -------------------------------------------------------------------------------- 1 | commands_formatters = {'formatters': ['plain', 'bold', 'italic', 'header', 'link', 'inline-code', 2 | 'ordered-list', 'unordered-list', 'new-line'], 3 | 'commands': ['!help', '!done']} 4 | markdown_text = '' 5 | while True: 6 | usr_command = input("Choose a formatter: ") 7 | if usr_command not in commands_formatters['formatters'] and usr_command not in commands_formatters['commands']: 8 | print("Unknown formatting type or command") 9 | elif usr_command == '!help': 10 | print('Available formatters: plain bold italic header link inline-code ordered-list unordered-list new-line\n' 11 | 'Special commands: !help !done') 12 | elif usr_command == '!done': 13 | with open('output.md', 'w') as file: 14 | file.write(markdown_text) 15 | break 16 | elif usr_command == 'header': 17 | while True: 18 | level = int(input('Level: ')) 19 | if level > 6 or level < 1: 20 | print('The level should be within the range of 1 to 6') 21 | else: 22 | text = input('Text: ') 23 | if len(markdown_text): 24 | markdown_text += f'\n' + level * f'#' + f' {text}\n' 25 | else: 26 | markdown_text += level * f'#' + f' {text}\n' 27 | break 28 | elif usr_command == 'plain': 29 | text = input('Text: ') 30 | markdown_text += f'{text}' 31 | elif usr_command == 'bold': 32 | text = input('Text: ') 33 | markdown_text += f'**{text}**' 34 | elif usr_command == 'italic': 35 | text = input('Text: ') 36 | markdown_text += f'*{text}*' 37 | elif usr_command == 'inline-code': 38 | text = input('Text: ') 39 | markdown_text += f'`{text}`' 40 | elif usr_command == 'new-line': 41 | markdown_text += f'\n' 42 | elif usr_command == 'link': 43 | label_text = input('Label: ') 44 | url_text = input('URL: ') 45 | markdown_text += f'[{label_text}]({url_text})' 46 | elif usr_command == 'unordered-list' or usr_command == 'ordered-list': 47 | rows_number = 0 48 | while rows_number < 1: 49 | rows_number = int(input('Number of rows: ')) 50 | print('The number of rows should be greater than zero') 51 | else: 52 | for i in range(1, rows_number+1): 53 | text = input(f'Row #{i}: ') 54 | if usr_command == 'unordered-list': 55 | markdown_text += f'* {text}\n' 56 | else: 57 | markdown_text += f'{i}. {text}\n' 58 | 59 | print(markdown_text) 60 | 61 | --------------------------------------------------------------------------------