eric6/QScintilla/Exporters/ExporterHTML.py

changeset 7267
aedc309827c7
parent 7229
53054eb5b15a
child 7305
928858251f7a
equal deleted inserted replaced
7266:d001bc703c29 7267:aedc309827c7
70 else: 70 else:
71 for index in range(QsciScintilla.STYLE_MAX + 1): 71 for index in range(QsciScintilla.STYLE_MAX + 1):
72 styleIsUsed[index] = True 72 styleIsUsed[index] = True
73 styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True 73 styleIsUsed[QsciScintilla.STYLE_DEFAULT] = True
74 74
75 html = \ 75 html = (
76 '''<!DOCTYPE html PUBLIC "-//W3C//DTD''' \ 76 '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
77 ''' XHTML 1.0 Transitional//EN"\n''' \ 77 ''' XHTML 1.0 Transitional//EN"\n'''
78 ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">''' \ 78 ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'''
79 '''\n''' \ 79 '''\n'''
80 '''<html xmlns="http://www.w3.org/1999/xhtml">\n''' \ 80 '''<html xmlns="http://www.w3.org/1999/xhtml">\n'''
81 '''<head>\n''' 81 '''<head>\n'''
82 )
82 if titleFullPath: 83 if titleFullPath:
83 html += '''<title>{0}</title>\n'''.format( 84 html += '''<title>{0}</title>\n'''.format(
84 self.editor.getFileName()) 85 self.editor.getFileName())
85 else: 86 else:
86 html += '''<title>{0}</title>\n'''.format( 87 html += '''<title>{0}</title>\n'''.format(
87 os.path.basename(self.editor.getFileName())) 88 os.path.basename(self.editor.getFileName()))
88 html += '''<meta name="Generator" content="eric6" />\n''' \ 89 html += (
89 '''<meta http-equiv="Content-Type" ''' \ 90 '''<meta name="Generator" content="eric6" />\n'''
91 '''<meta http-equiv="Content-Type" '''
90 '''content="text/html; charset=utf-8" />\n''' 92 '''content="text/html; charset=utf-8" />\n'''
93 )
91 if folding: 94 if folding:
92 html += \ 95 html += (
93 '''<script language="JavaScript" type="text/javascript">\n''' \ 96 '''<script language="JavaScript" type="text/javascript">\n'''
94 '''<!--\n''' \ 97 '''<!--\n'''
95 '''function symbol(id, sym) {\n''' \ 98 '''function symbol(id, sym) {\n'''
96 ''' if (id.textContent == undefined) {\n''' \ 99 ''' if (id.textContent == undefined) {\n'''
97 ''' id.innerText = sym;\n''' \ 100 ''' id.innerText = sym;\n'''
98 ''' } else {\n''' \ 101 ''' } else {\n'''
99 ''' id.textContent = sym;\n''' \ 102 ''' id.textContent = sym;\n'''
100 ''' }\n''' \ 103 ''' }\n'''
101 '''}\n''' \ 104 '''}\n'''
102 '''function toggle(id) {\n''' \ 105 '''function toggle(id) {\n'''
103 ''' var thislayer = document.getElementById('ln' + id);\n''' \ 106 ''' var thislayer = document.getElementById('ln' + id);\n'''
104 ''' id -= 1;\n''' \ 107 ''' id -= 1;\n'''
105 ''' var togline = document.getElementById('hd' + id);\n''' \ 108 ''' var togline = document.getElementById('hd' + id);\n'''
106 ''' var togsym = document.getElementById('bt' + id);\n''' \ 109 ''' var togsym = document.getElementById('bt' + id);\n'''
107 ''' if (thislayer.style.display == 'none') {\n''' \ 110 ''' if (thislayer.style.display == 'none') {\n'''
108 ''' thislayer.style.display = 'block';\n''' \ 111 ''' thislayer.style.display = 'block';\n'''
109 ''' togline.style.textDecoration = 'none';\n''' \ 112 ''' togline.style.textDecoration = 'none';\n'''
110 ''' symbol(togsym, '- ');\n''' \ 113 ''' symbol(togsym, '- ');\n'''
111 ''' } else {\n''' \ 114 ''' } else {\n'''
112 ''' thislayer.style.display = 'none';\n''' \ 115 ''' thislayer.style.display = 'none';\n'''
113 ''' togline.style.textDecoration = 'underline';\n''' \ 116 ''' togline.style.textDecoration = 'underline';\n'''
114 ''' symbol(togsym, '+ ');\n''' \ 117 ''' symbol(togsym, '+ ');\n'''
115 ''' }\n''' \ 118 ''' }\n'''
116 '''}\n''' \ 119 '''}\n'''
117 '''//-->\n''' \ 120 '''//-->\n'''
118 '''</script>\n''' 121 '''</script>\n'''
122 )
119 123
120 lex = self.editor.getLexer() 124 lex = self.editor.getLexer()
121 if lex: 125 if lex:
122 bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT).name() 126 bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT).name()
123 else: 127 else:
125 129
126 html += '''<style type="text/css">\n''' 130 html += '''<style type="text/css">\n'''
127 if lex: 131 if lex:
128 istyle = 0 132 istyle = 0
129 while istyle <= QsciScintilla.STYLE_MAX: 133 while istyle <= QsciScintilla.STYLE_MAX:
130 if (istyle <= QsciScintilla.STYLE_DEFAULT or 134 if (
131 istyle > QsciScintilla.STYLE_LASTPREDEFINED) and \ 135 (istyle <= QsciScintilla.STYLE_DEFAULT or
132 styleIsUsed[istyle]: 136 istyle > QsciScintilla.STYLE_LASTPREDEFINED) and
133 if lex.description(istyle) or \ 137 styleIsUsed[istyle]
134 istyle == QsciScintilla.STYLE_DEFAULT: 138 ):
139 if (
140 lex.description(istyle) or
141 istyle == QsciScintilla.STYLE_DEFAULT
142 ):
135 font = lex.font(istyle) 143 font = lex.font(istyle)
136 colour = lex.color(istyle) 144 colour = lex.color(istyle)
137 paper = lex.paper(istyle) 145 paper = lex.paper(istyle)
138 if istyle == QsciScintilla.STYLE_DEFAULT: 146 if istyle == QsciScintilla.STYLE_DEFAULT:
139 html += '''span {\n''' 147 html += '''span {\n'''
145 html += ''' font-weight: bold;\n''' 153 html += ''' font-weight: bold;\n'''
146 if wysiwyg: 154 if wysiwyg:
147 html += ''' font-family: '{0}';\n'''.format( 155 html += ''' font-family: '{0}';\n'''.format(
148 font.family()) 156 font.family())
149 html += ''' color: {0};\n'''.format(colour.name()) 157 html += ''' color: {0};\n'''.format(colour.name())
150 if istyle != QsciScintilla.STYLE_DEFAULT and \ 158 if (
151 bgColour != paper.name(): 159 istyle != QsciScintilla.STYLE_DEFAULT and
160 bgColour != paper.name()
161 ):
152 html += ''' background: {0};\n'''.format( 162 html += ''' background: {0};\n'''.format(
153 paper.name()) 163 paper.name())
154 html += ''' text-decoration: inherit;\n''' 164 html += ''' text-decoration: inherit;\n'''
155 if wysiwyg: 165 if wysiwyg:
156 html += ''' font-size: {0:d}pt;\n'''.format( 166 html += ''' font-size: {0:d}pt;\n'''.format(
193 html += '''<span>''' 203 html += '''<span>'''
194 else: 204 else:
195 html += '''<pre>''' 205 html += '''<pre>'''
196 206
197 if folding: 207 if folding:
198 if self.editor.foldFlagsAt(line) & \ 208 if (
199 QsciScintilla.SC_FOLDLEVELHEADERFLAG: 209 self.editor.foldFlagsAt(line) &
200 html += '''<span id="hd{0:d}" onclick="toggle('{1:d}')">'''\ 210 QsciScintilla.SC_FOLDLEVELHEADERFLAG
201 .format(line, line + 1) 211 ):
212 html += (
213 '''<span id="hd{0:d}" onclick="toggle('{1:d}')">'''
214 ).format(line, line + 1)
202 html += '''<span id="bt{0:d}">- </span>'''.format(line) 215 html += '''<span id="bt{0:d}">- </span>'''.format(line)
203 inFoldSpan = True 216 inFoldSpan = True
204 else: 217 else:
205 html += '''&nbsp; ''' 218 html += '''&nbsp; '''
206 219
283 levelStack.pop() 296 levelStack.pop()
284 html += '\n' # here to get clean code 297 html += '\n' # here to get clean code
285 if newLevel > level: 298 if newLevel > level:
286 html += '''<span id="ln{0:d}">'''.format(line) 299 html += '''<span id="ln{0:d}">'''.format(line)
287 levelStack.append(newLevel) 300 levelStack.append(newLevel)
288 if self.editor.foldFlagsAt(line) & \ 301 if (
289 QsciScintilla.SC_FOLDLEVELHEADERFLAG: 302 self.editor.foldFlagsAt(line) &
290 html += \ 303 QsciScintilla.SC_FOLDLEVELHEADERFLAG
291 '''<span id="hd{0:d}"''' \ 304 ):
292 ''' onclick="toggle('{1:d}')">''' \ 305 html += (
293 .format(line, line + 1) 306 '''<span id="hd{0:d}"'''
307 ''' onclick="toggle('{1:d}')">'''
308 ).format(line, line + 1)
294 html += '''<span id="bt{0:d}">- </span>'''.format(line) 309 html += '''<span id="bt{0:d}">- </span>'''.format(line)
295 inFoldSpan = True 310 inFoldSpan = True
296 else: 311 else:
297 html += '''&nbsp; ''' 312 html += '''&nbsp; '''
298 level = newLevel 313 level = newLevel
299 else: 314 else:
300 html += '\n' 315 html += '\n'
301 316
302 if styleIsUsed[styleCurrent] and \ 317 if (
303 self.editor.byteAt(pos + 1) not in [b'\r', b'\n']: 318 styleIsUsed[styleCurrent] and
319 self.editor.byteAt(pos + 1) not in [b'\r', b'\n']
320 ):
304 # We know it's the correct next style, 321 # We know it's the correct next style,
305 # but no (empty) span for an empty line 322 # but no (empty) span for an empty line
306 html += '''<span class="S{0:0d}">'''.format(styleCurrent) 323 html += '''<span class="S{0:0d}">'''.format(styleCurrent)
307 inStyleSpan = True 324 inStyleSpan = True
308 else: 325 else:
383 if fn: 400 if fn:
384 extension = os.path.normcase(os.path.splitext(fn)[1][1:]) 401 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
385 else: 402 else:
386 extension = "" 403 extension = ""
387 404
388 if extension in \ 405 if (
389 Preferences.getEditor("PreviewMarkdownFileNameExtensions") or \ 406 extension in Preferences.getEditor(
390 self.editor.getLanguage().lower() == "markdown": 407 "PreviewMarkdownFileNameExtensions") or
408 self.editor.getLanguage().lower() == "markdown"
409 ):
391 # export markdown to HTML 410 # export markdown to HTML
392 html = self.__generateFromMarkdown() 411 html = self.__generateFromMarkdown()
393 elif extension in \ 412 elif (
394 Preferences.getEditor("PreviewRestFileNameExtensions") or \ 413 extension in Preferences.getEditor(
395 self.editor.getLanguage().lower() == "restructuredtext": 414 "PreviewRestFileNameExtensions") or
415 self.editor.getLanguage().lower() == "restructuredtext"
416 ):
396 # export ReST to HTML 417 # export ReST to HTML
397 html = self.__generateFromReSTDocutils() 418 html = self.__generateFromReSTDocutils()
398 else: 419 else:
399 tabSize = self.editor.getEditorConfig("TabWidth") 420 tabSize = self.editor.getEditorConfig("TabWidth")
400 if tabSize == 0: 421 if tabSize == 0:
515 extensions = ['fenced_code', 'nl2br', 'extra'] 536 extensions = ['fenced_code', 'nl2br', 'extra']
516 else: 537 else:
517 extensions = ['fenced_code', 'extra'] 538 extensions = ['fenced_code', 'extra']
518 539
519 # version 2.0 supports only extension names, not instances 540 # version 2.0 supports only extension names, not instances
520 if markdown.version_info[0] > 2 or \ 541 if (
521 (markdown.version_info[0] == 2 and 542 markdown.version_info[0] > 2 or
522 markdown.version_info[1] > 0): 543 (markdown.version_info[0] == 2 and
544 markdown.version_info[1] > 0)
545 ):
523 class _StrikeThroughExtension(markdown.Extension): 546 class _StrikeThroughExtension(markdown.Extension):
524 """ 547 """
525 Class is placed here, because it depends on imported markdown, 548 Class is placed here, because it depends on imported markdown,
526 and markdown import is lazy. 549 and markdown import is lazy.
527 550
552 body = markdown.markdown(self.editor.text(), 575 body = markdown.markdown(self.editor.text(),
553 extensions=extensions, 576 extensions=extensions,
554 output_format=htmlFormat) 577 output_format=htmlFormat)
555 578
556 if htmlFormat == "xhtml1": 579 if htmlFormat == "xhtml1":
557 head = \ 580 head = (
558 '''<!DOCTYPE html PUBLIC "-//W3C//DTD''' \ 581 '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
559 ''' XHTML 1.0 Transitional//EN"\n''' \ 582 ''' XHTML 1.0 Transitional//EN"\n'''
560 ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional''' \ 583 ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional'''
561 '''.dtd">\n''' \ 584 '''.dtd">\n'''
562 '''<html xmlns="http://www.w3.org/1999/xhtml">\n''' 585 '''<html xmlns="http://www.w3.org/1999/xhtml">\n'''
586 )
563 elif htmlFormat == "html5": 587 elif htmlFormat == "html5":
564 head = \ 588 head = (
565 '''<!DOCTYPE html>\n''' \ 589 '''<!DOCTYPE html>\n'''
566 '''<html lang="EN">\n''' 590 '''<html lang="EN">\n'''
591 )
567 else: 592 else:
568 head = '<html lang="EN">\n' 593 head = '<html lang="EN">\n'
569 head += '''<head>\n''' 594 head += '''<head>\n'''
570 if Preferences.getEditorExporter("HTML/FullPathAsTitle"): 595 if Preferences.getEditorExporter("HTML/FullPathAsTitle"):
571 head += '''<title>{0}</title>\n'''.format( 596 head += '''<title>{0}</title>\n'''.format(
572 self.editor.getFileName()) 597 self.editor.getFileName())
573 else: 598 else:
574 head += '''<title>{0}</title>\n'''.format( 599 head += '''<title>{0}</title>\n'''.format(
575 os.path.basename(self.editor.getFileName())) 600 os.path.basename(self.editor.getFileName()))
576 head += '''<meta name="Generator" content="eric6" />\n''' \ 601 head += (
577 '''<meta http-equiv="Content-Type" ''' \ 602 '''<meta name="Generator" content="eric6" />\n'''
578 '''content="text/html; charset=utf-8" />\n''' \ 603 '''<meta http-equiv="Content-Type" '''
579 '''</head>\n''' \ 604 '''content="text/html; charset=utf-8" />\n'''
605 '''</head>\n'''
580 '''<body>\n''' 606 '''<body>\n'''
607 )
581 608
582 foot = '''\n</body>\n</html>\n''' 609 foot = '''\n</body>\n</html>\n'''
583 610
584 return head + body + foot 611 return head + body + foot

eric ide

mercurial