1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| import re
import os
from lxml import etree
from jinja2 import Template
icon_svg_filename = { '#d': 'Description', '#e': 'Example', '#t': 'Transfer', '#c': 'Custom', '#v': 'Verification', '#a': 'Advertisement' }
icon_svg_changes = { 'fill': 'currentColor', 'width': '2em', 'height': '2em' }
card_color = { '#d': '200 72% 50%', '#e': '0 72% 50%', '#t': '50 72% 50%', '#c': '0 0% 50%', '#v': '260 72% 50%', '#a': '300 72% 50%' }
def extract_modify_svg_content(svg_filename, svg_changes): """ 从SVG文件中提取并修改内容。 Parameters: svg_filename (str): SVG文件名。 svg_changes: 变化参数 Returns: str_content: 修改后的SVG内容。 """ svg_path = os.path.join('svg', f'{svg_filename}.svg') with open(svg_path, 'r', encoding='utf-8') as svg_file: svg_code = svg_file.read() svg_bytes = svg_code.encode('utf-8') svg_root = etree.fromstring(svg_bytes) for key, value in svg_changes.items(): svg_root.attrib[key] = value svg_content = etree.tostring(svg_root, encoding="unicode") return svg_content
def convert_evoldown_to_markdown(evoldown_file, markdown_file): """ 将Evoldown格式转换为Markdown格式。 Parameters: evoldown_file (str): 输入的Evoldown文件位置。 markdown_file (str): 输出的Markdown文件位置。 """ with open(evoldown_file, 'r', encoding='utf-8') as input_file: evoldown_text = input_file.read() pattern = r'(#[detva])( \S+)( \S+)?' pattern_c = r'(#c)( \S+)( \S+)( \S+)?' matches = re.findall(pattern, evoldown_text) matches_c = re.findall(pattern_c, evoldown_text) dict_matches = {} dict_matches_c = {} for match in matches: material_type, keywords, associated_keyword = match dict_matches[keywords] = material_type for match_c in matches_c: material_type, custom_type, keywords, associated_keyword = match_c dict_matches_c[keywords] = [material_type, custom_type] def find_associated_type(key): if key in dict_matches: return [dict_matches[key], None] elif key in dict_matches_c: return dict_matches_c[key] new_markdown_text = evoldown_text tag_html_template = """ <div style="display: flex; flex-direction: row; align-items: center;"> {{ material_card }} {{ find_associated_svg }} {{ associated_card }} </div> """ card_html_template = """<span style="display: flex; flex-direction: row; align-items: center; color: hsl({{ color_value }}); background-color: hsl({{ color_value }}/0.08); padding: 0 0.4em; border-radius: 0.4em;; line-height: 2.5em"> <span style="display: flex; flex-direction: row; align-items: center; margin-right: 0.4em; "> {{ svg_code }} {% if custom_type %} <span style="">{{ custom_type }} |</span> {% endif %} </span> {{ keywords }} </span> """ tag_template = Template(tag_html_template) card_template = Template(card_html_template) """ 翻译参考 Learning material tags, learning material types, learning material type cards, custom learning material types, learning material keywords, associated_keyword learning materials. No association found 学习材料标记、学习材料的类型、学习材料类型卡、自定义的学习材料的类型、学习材料的关键词、关联的学习材料。未发现关联 """ for match in matches: material_type, keywords, associated_keyword = match material_card = card_template.render( color_value=card_color[material_type], svg_code=extract_modify_svg_content(icon_svg_filename[material_type], icon_svg_changes), keywords=keywords.strip(), ) if associated_keyword: associated_types = find_associated_type(associated_keyword) if associated_types: associated_type = associated_types[0] associated_custom_type = associated_types[1] associated_card = card_template.render( color_value=card_color[associated_type], svg_code=extract_modify_svg_content(icon_svg_filename[associated_type], icon_svg_changes), custom_type=associated_custom_type, keywords=associated_keyword ) find_associated_svg = extract_modify_svg_content('Associated', icon_svg_changes) else: associated_card = card_template.render( color_value='0 0% 70%', keywords=associated_keyword ) find_associated_svg = extract_modify_svg_content('NotFoundAssociated', icon_svg_changes) else: associated_card = '' find_associated_svg = '' tag_html_code = tag_template.render( material_card=material_card, find_associated_svg=find_associated_svg, associated_card=associated_card, ) new_markdown_text = new_markdown_text.replace( f'\n{material_type}{keywords}{associated_keyword}\n', tag_html_code) for match_c in matches_c: material_type, custom_type, keywords, associated_keyword = match_c material_card = card_template.render( color_value=card_color[material_type], svg_code=extract_modify_svg_content(icon_svg_filename[material_type], icon_svg_changes), custom_type=custom_type, keywords=keywords.strip(), ) if associated_keyword: associated_types = find_associated_type(associated_keyword) if associated_types: associated_type = associated_types[0] associated_custom_type = associated_types[1] associated_card = card_template.render( color_value=card_color[associated_type], svg_code=extract_modify_svg_content(icon_svg_filename[associated_type], icon_svg_changes), custom_type=associated_custom_type, keywords=associated_keyword ) find_associated_svg = extract_modify_svg_content('Associated', icon_svg_changes) else: associated_card = card_template.render( color_value='0 0% 70%', keywords=associated_keyword ) find_associated_svg = extract_modify_svg_content('NotFoundAssociated', icon_svg_changes) else: associated_card = '' find_associated_svg = '' tag_html_code = tag_template.render( material_card=material_card, find_associated_svg=find_associated_svg, associated_card=associated_card, ) new_markdown_text = new_markdown_text.replace( f'\n{material_type}{custom_type}{keywords}{associated_keyword}\n', tag_html_code) with open(markdown_file, 'w', encoding='utf-8') as output_file: output_file.write(new_markdown_text)
convert_evoldown_to_markdown(r'markdown\example.md', r'markdown\example_output.md')
|