daswer123 commited on
Commit
078908e
·
verified ·
1 Parent(s): 651a7ed

Upload 2 files

Browse files
Files changed (2) hide show
  1. converter.py +33 -1
  2. requirements.txt +3 -3
converter.py CHANGED
@@ -1,5 +1,8 @@
1
  import re
2
  import json
 
 
 
3
  from pathlib import Path
4
  from typing import List, Dict, Any, Optional, Tuple
5
  import markdown
@@ -203,7 +206,10 @@ class MarkdownToDocxConverter:
203
  language = cls.replace('language-', '')
204
  break
205
  code_text = code_element.get_text()
206
- self.add_code_block(code_text, language)
 
 
 
207
  else:
208
  self.add_code_block(element.get_text())
209
  elif element.name == 'code' and parent_paragraph:
@@ -241,6 +247,32 @@ class MarkdownToDocxConverter:
241
  for child in element.children:
242
  self.process_html_element(child, parent_paragraph)
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  def add_table(self, table_element):
245
  """Добавление таблицы в документ"""
246
  rows = table_element.find_all('tr')
 
1
  import re
2
  import json
3
+ import io
4
+ import requests
5
+ import base64
6
  from pathlib import Path
7
  from typing import List, Dict, Any, Optional, Tuple
8
  import markdown
 
206
  language = cls.replace('language-', '')
207
  break
208
  code_text = code_element.get_text()
209
+ if language == 'mermaid':
210
+ self.add_mermaid_diagram(code_text)
211
+ else:
212
+ self.add_code_block(code_text, language)
213
  else:
214
  self.add_code_block(element.get_text())
215
  elif element.name == 'code' and parent_paragraph:
 
247
  for child in element.children:
248
  self.process_html_element(child, parent_paragraph)
249
 
250
+ def add_mermaid_diagram(self, code: str):
251
+ """Рендеринг и вставка диаграммы Mermaid"""
252
+ try:
253
+ # Кодируем код диаграммы в base64
254
+ graphbytes = code.encode("ascii")
255
+ base64_bytes = base64.b64encode(graphbytes)
256
+ base64_string = base64_bytes.decode("ascii")
257
+
258
+ # Формируем URL для запроса
259
+ url = f"https://mermaid.ink/img/{base64_string}"
260
+
261
+ # Выполняем запрос и получаем изображение
262
+ response = requests.get(url)
263
+ response.raise_for_status() # Проверка на ошибки HTTP
264
+
265
+ # Вставляем изображение в документ
266
+ image_stream = io.BytesIO(response.content)
267
+ self.doc.add_picture(image_stream, width=Inches(6))
268
+
269
+ except requests.exceptions.RequestException as e:
270
+ print(f"Ошибка при рендеринге диаграммы Mermaid: {e}")
271
+ self.doc.add_paragraph(f"[Ошибка рендеринга диаграммы: {e}]", style='Body Text')
272
+ except Exception as e:
273
+ print(f"Неожиданная ошибка при обработке Mermaid: {e}")
274
+ self.doc.add_paragraph(f"[Неожиданная ошибка: {e}]", style='Body Text')
275
+
276
  def add_table(self, table_element):
277
  """Добавление таблицы в документ"""
278
  rows = table_element.find_all('tr')
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- python-docx
2
- gradio
3
  markdown
4
- beautifulsoup4
 
 
 
 
 
1
  markdown
2
+ python-docx
3
+ beautifulsoup4
4
+ requests