-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_viewer.py
More file actions
247 lines (194 loc) · 7.73 KB
/
tutorial_viewer.py
File metadata and controls
247 lines (194 loc) · 7.73 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tutorial Viewer - Simple markdown to HTML converter
"""
import os
import re
import tempfile
import webbrowser
from pathlib import Path
class MarkdownToHTMLConverter:
"""Simple markdown to HTML converter"""
def __init__(self):
self.html_template = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{title}</title>
<style>
body {{
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 40px auto;
padding: 20px;
color: #333;
}}
h1 {{ margin-top: 30px; border-bottom: 2px solid #333; padding-bottom: 10px; }}
h2 {{ margin-top: 25px; border-bottom: 1px solid #666; padding-bottom: 8px; }}
h3 {{ margin-top: 20px; }}
code {{ background: #f4f4f4; padding: 2px 5px; font-family: monospace; }}
pre {{ background: #f4f4f4; padding: 10px; overflow-x: auto; }}
pre code {{ background: none; padding: 0; }}
ul, ol {{ margin-left: 25px; }}
li {{ margin-bottom: 5px; }}
blockquote {{ border-left: 3px solid #666; padding-left: 15px; margin: 15px 0; color: #666; }}
a {{ color: #333; }}
</style>
</head>
<body>
{content}
</body>
</html>
"""
def convert(self, markdown_text):
"""Convert markdown to HTML"""
html = markdown_text
# Escape HTML entities first
# html = html.replace('&', '&').replace('<', '<').replace('>', '>')
# Headers
html = re.sub(r'^# (.+)$', r'<h1>\1</h1>', html, flags=re.MULTILINE)
html = re.sub(r'^## (.+)$', r'<h2>\1</h2>', html, flags=re.MULTILINE)
html = re.sub(r'^### (.+)$', r'<h3>\1</h3>', html, flags=re.MULTILINE)
html = re.sub(r'^#### (.+)$', r'<h4>\1</h4>', html, flags=re.MULTILINE)
# Horizontal rules
html = re.sub(r'^---$', r'<hr>', html, flags=re.MULTILINE)
# Bold and italic
html = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', html)
html = re.sub(r'\*(.+?)\*', r'<em>\1</em>', html)
# Inline code
html = re.sub(r'`([^`]+)`', r'<code>\1</code>', html)
# Links
html = re.sub(r'\[([^\]]+)\]\(([^\)]+)\)', r'<a href="\2" target="_blank">\1</a>', html)
# Lists - unordered
lines = html.split('\n')
in_ul = False
result = []
for i, line in enumerate(lines):
# Check for unordered list
if re.match(r'^- (.+)$', line):
if not in_ul:
result.append('<ul>')
in_ul = True
content = re.sub(r'^- (.+)$', r'\1', line)
result.append(f'<li>{content}</li>')
else:
if in_ul:
result.append('</ul>')
in_ul = False
result.append(line)
if in_ul:
result.append('</ul>')
html = '\n'.join(result)
# Ordered lists
lines = html.split('\n')
in_ol = False
result = []
for line in lines:
if re.match(r'^\d+\. (.+)$', line):
if not in_ol:
result.append('<ol>')
in_ol = True
content = re.sub(r'^\d+\. (.+)$', r'\1', line)
result.append(f'<li>{content}</li>')
else:
if in_ol:
result.append('</ol>')
in_ol = False
result.append(line)
if in_ol:
result.append('</ol>')
html = '\n'.join(result)
# Paragraphs
lines = html.split('\n')
result = []
in_p = False
for line in lines:
stripped = line.strip()
# Skip if it's already an HTML tag
if stripped.startswith('<') or stripped == '':
if in_p:
result.append('</p>')
in_p = False
result.append(line)
else:
if not in_p:
result.append('<p>')
in_p = True
result.append(line)
if in_p:
result.append('</p>')
html = '\n'.join(result)
return html
def create_html_file(self, markdown_file_path):
"""Create HTML file from markdown file"""
try:
with open(markdown_file_path, 'r', encoding='utf-8') as f:
markdown_content = f.read()
# Extract title from first line
title_match = re.match(r'^# (.+)$', markdown_content, re.MULTILINE)
title = title_match.group(1) if title_match else "Ask AI Plugin Tutorial"
# Convert markdown to HTML
html_content = self.convert(markdown_content)
# Create full HTML
full_html = self.html_template.format(
title=title,
content=html_content
)
# Create temporary HTML file
temp_dir = tempfile.gettempdir()
html_file_path = os.path.join(temp_dir, 'ask_ai_plugin_tutorial.html')
with open(html_file_path, 'w', encoding='utf-8') as f:
f.write(full_html)
return html_file_path
except Exception as e:
raise Exception(f"Failed to create HTML file: {str(e)}")
def open_tutorial_in_browser():
"""Open the tutorial in the default web browser"""
import logging
logger = logging.getLogger(__name__)
try:
# Get plugin instance to use get_resources
from calibre.customize.ui import find_plugin
plugin = find_plugin('Ask AI Plugin')
if not plugin:
logger.error("Plugin not found")
return False
# Read tutorial using plugin's get_resources method
tutorial_data = plugin.get_resources('tutorial/tutorial_v0.8.md')
if not tutorial_data:
tutorial_data = plugin.get_resources('tutorial/tutorial_v0.7.md')
if not tutorial_data:
logger.error("Failed to read tutorial")
return False
tutorial_content = tutorial_data.decode('utf-8')
logger.info(f"Read tutorial: {len(tutorial_content)} bytes")
# Convert to HTML
converter = MarkdownToHTMLConverter()
# Extract title
title_match = re.match(r'^# (.+)$', tutorial_content, re.MULTILINE)
title = title_match.group(1) if title_match else "Ask AI Plugin Tutorial"
# Convert markdown to HTML
html_content = converter.convert(tutorial_content)
# Create full HTML
full_html = converter.html_template.format(
title=title,
content=html_content
)
# Create temporary HTML file using calibre's temp directory
from calibre.ptempfile import PersistentTemporaryFile
# Create a persistent temp file that won't be deleted immediately
with PersistentTemporaryFile(suffix='.html', prefix='ask_ai_plugin_tutorial_') as f:
f.write(full_html.encode('utf-8'))
html_file_path = f.name
logger.info(f"Created HTML: {html_file_path}")
# Open in browser
webbrowser.open(f'file://{html_file_path}')
return True
except Exception as e:
logger.error(f"Error: {str(e)}")
return False
if __name__ == '__main__':
# Test the converter
open_tutorial_in_browser()