Line data Source code
1 : /*
2 : * Copyright 2014 Christian Loose <christian.loose@hamburg.de>
3 : *
4 : * Redistribution and use in source and binary forms, with or without
5 : * modification, are permitted provided that the following conditions are
6 : * met:
7 : *
8 : * (1) Redistributions of source code must retain the above copyright
9 : * notice, this list of conditions and the following disclaimer.
10 : *
11 : * (2) Redistributions in binary form must reproduce the above copyright
12 : * notice, this list of conditions and the following disclaimer in
13 : * the documentation and/or other materials provided with the
14 : * distribution.
15 : *
16 : * (3) The name of the author may not be used to
17 : * endorse or promote products derived from this software without
18 : * specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 : * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 : * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 : * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 : * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 : * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 : * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 : * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 : * POSSIBILITY OF SUCH DAMAGE.
31 : */
32 : #include "htmltemplate.h"
33 :
34 : #include <QFile>
35 :
36 0 : HtmlTemplate::HtmlTemplate ()
37 : {
38 0 : }
39 :
40 0 : QString HtmlTemplate::render (const QString & body, RenderOptions options) const
41 : {
42 : // add scrollbar synchronization
43 0 : options |= Template::ScrollbarSynchronization;
44 :
45 0 : return renderAsHtml (QString (), body, options);
46 : }
47 :
48 0 : QString HtmlTemplate::exportAsHtml (const QString & header, const QString & body, RenderOptions options) const
49 : {
50 : // clear code highlighting option since it depends on the resource file
51 0 : options &= ~Template::CodeHighlighting;
52 :
53 0 : return renderAsHtml (header, body, options);
54 : }
55 :
56 0 : QString HtmlTemplate::renderAsHtml (const QString & header, const QString & body, Template::RenderOptions options) const
57 : {
58 0 : if (htmlTemplate.isEmpty ())
59 : {
60 : return body;
61 : }
62 :
63 0 : QString htmlHeader = buildHtmlHeader (options);
64 0 : htmlHeader += header;
65 :
66 0 : return QString (htmlTemplate)
67 0 : .replace (QLatin1String ("<!--__HTML_HEADER__-->"), htmlHeader)
68 0 : .replace (QLatin1String ("<!--__HTML_CONTENT__-->"), body);
69 : }
70 :
71 0 : QString HtmlTemplate::buildHtmlHeader (RenderOptions options) const
72 : {
73 0 : QString header;
74 :
75 : // add javascript for scrollbar synchronization
76 0 : if (options.testFlag (Template::ScrollbarSynchronization))
77 : {
78 0 : header += "<script type=\"text/javascript\">window.onscroll = function() { mainwin.webViewScrolled(); }; </script>\n";
79 : }
80 :
81 : // add MathJax.js script to HTML header
82 0 : if (options.testFlag (Template::MathSupport))
83 : {
84 : header +=
85 : "<script type=\"text/javascript\" "
86 0 : "src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>\n";
87 : }
88 :
89 : // add Highlight.js script to HTML header
90 0 : if (options.testFlag (Template::CodeHighlighting))
91 : {
92 0 : header += QString ("<link rel=\"stylesheet\" href=\"qrc:/scripts/highlight.js/styles/%1.css\">\n")
93 0 : .arg (codeHighlightingStyle ());
94 0 : header += "<script src=\"qrc:/scripts/highlight.js/highlight.pack.js\"></script>\n";
95 0 : header += "<script>hljs.initHighlightingOnLoad();</script>\n";
96 : }
97 :
98 0 : return header;
99 : }
|