export: add option EmbedCssStyle for HTML

This commit is contained in:
Le Tan 2018-02-25 14:37:13 +08:00
parent 7069f7268b
commit b7e6301136
9 changed files with 75 additions and 19 deletions

View File

@ -150,9 +150,11 @@ void VExportDialog::setupUI()
m_basicBox->setLayout(basicLayout); m_basicBox->setLayout(basicLayout);
// Settings box. // Settings box.
m_htmlSettings = setupHTMLAdvancedSettings();
m_pdfSettings = setupPDFAdvancedSettings(); m_pdfSettings = setupPDFAdvancedSettings();
QVBoxLayout *advLayout = new QVBoxLayout(); QVBoxLayout *advLayout = new QVBoxLayout();
advLayout->addWidget(m_htmlSettings);
advLayout->addWidget(m_pdfSettings); advLayout->addWidget(m_pdfSettings);
m_settingBox->setLayout(advLayout); m_settingBox->setLayout(advLayout);
@ -172,20 +174,20 @@ QWidget *VExportDialog::setupPDFAdvancedSettings()
{ {
// Page layout settings. // Page layout settings.
m_layoutLabel = new QLabel(); m_layoutLabel = new QLabel();
m_layoutBtn = new QPushButton(tr("Settings")); QPushButton *layoutBtn = new QPushButton(tr("Settings"));
#ifndef QT_NO_PRINTER #ifndef QT_NO_PRINTER
connect(m_layoutBtn, &QPushButton::clicked, connect(layoutBtn, &QPushButton::clicked,
this, &VExportDialog::handleLayoutBtnClicked); this, &VExportDialog::handleLayoutBtnClicked);
#else #else
m_layoutBtn->hide(); layoutBtn->hide();
#endif #endif
updatePageLayoutLabel(); updatePageLayoutLabel();
QHBoxLayout *layoutLayout = new QHBoxLayout(); QHBoxLayout *layoutLayout = new QHBoxLayout();
layoutLayout->addWidget(m_layoutLabel); layoutLayout->addWidget(m_layoutLabel);
layoutLayout->addWidget(m_layoutBtn); layoutLayout->addWidget(layoutBtn);
layoutLayout->addStretch(); layoutLayout->addStretch();
QFormLayout *advLayout = new QFormLayout(); QFormLayout *advLayout = new QFormLayout();
@ -199,6 +201,24 @@ QWidget *VExportDialog::setupPDFAdvancedSettings()
return wid; return wid;
} }
QWidget *VExportDialog::setupHTMLAdvancedSettings()
{
// Embed CSS styles.
m_embedStyleCB = new QCheckBox(tr("Embed CSS styles"), this);
m_embedStyleCB->setToolTip(tr("Embed CSS styles in HTML file"));
m_embedStyleCB->setChecked(true);
QFormLayout *advLayout = new QFormLayout();
advLayout->addRow(m_embedStyleCB);
advLayout->setContentsMargins(0, 0, 0, 0);
QWidget *wid = new QWidget();
wid->setLayout(advLayout);
return wid;
}
void VExportDialog::initUIFields(MarkdownConverterType p_renderer) void VExportDialog::initUIFields(MarkdownConverterType p_renderer)
{ {
// Notes to export. // Notes to export.
@ -282,7 +302,8 @@ void VExportDialog::startExport()
m_renderBgCB->currentData().toString(), m_renderBgCB->currentData().toString(),
m_renderStyleCB->currentData().toString(), m_renderStyleCB->currentData().toString(),
m_renderCodeBlockStyleCB->currentData().toString(), m_renderCodeBlockStyleCB->currentData().toString(),
&m_pageLayout); &m_pageLayout,
m_embedStyleCB->isChecked());
s_lastExportFormat = opt.m_format; s_lastExportFormat = opt.m_format;
@ -709,6 +730,7 @@ void VExportDialog::updatePageLayoutLabel()
void VExportDialog::handleCurrentFormatChanged(int p_index) void VExportDialog::handleCurrentFormatChanged(int p_index)
{ {
bool pdfEnabled = false; bool pdfEnabled = false;
bool htmlEnabled = false;
if (p_index >= 0) { if (p_index >= 0) {
switch (m_formatCB->currentData().toInt()) { switch (m_formatCB->currentData().toInt()) {
@ -716,10 +738,15 @@ void VExportDialog::handleCurrentFormatChanged(int p_index)
pdfEnabled = true; pdfEnabled = true;
break; break;
case (int)ExportFormat::HTML:
htmlEnabled = true;
break;
default: default:
break; break;
} }
} }
m_pdfSettings->setVisible(pdfEnabled); m_pdfSettings->setVisible(pdfEnabled);
m_htmlSettings->setVisible(htmlEnabled);
} }

View File

@ -18,6 +18,7 @@ class VDirectory;
class VFile; class VFile;
class VCart; class VCart;
class VExporter; class VExporter;
class QCheckBox;
enum class ExportSource enum class ExportSource
@ -45,14 +46,16 @@ struct ExportOption
const QString &p_renderBg, const QString &p_renderBg,
const QString &p_renderStyle, const QString &p_renderStyle,
const QString &p_renderCodeBlockStyle, const QString &p_renderCodeBlockStyle,
QPageLayout *p_layout) QPageLayout *p_layout,
bool p_embedCssStyle)
: m_source(p_source), : m_source(p_source),
m_format(p_format), m_format(p_format),
m_renderer(p_renderer), m_renderer(p_renderer),
m_renderBg(p_renderBg), m_renderBg(p_renderBg),
m_renderStyle(p_renderStyle), m_renderStyle(p_renderStyle),
m_renderCodeBlockStyle(p_renderCodeBlockStyle), m_renderCodeBlockStyle(p_renderCodeBlockStyle),
m_layout(p_layout) m_layout(p_layout),
m_embedCssStyle(p_embedCssStyle)
{ {
} }
@ -66,6 +69,8 @@ struct ExportOption
QString m_renderStyle; QString m_renderStyle;
QString m_renderCodeBlockStyle; QString m_renderCodeBlockStyle;
QPageLayout *m_layout; QPageLayout *m_layout;
bool m_embedCssStyle;
}; };
@ -96,6 +101,8 @@ private:
QWidget *setupPDFAdvancedSettings(); QWidget *setupPDFAdvancedSettings();
QWidget *setupHTMLAdvancedSettings();
void initUIFields(MarkdownConverterType p_renderer); void initUIFields(MarkdownConverterType p_renderer);
QString getOutputDirectory() const; QString getOutputDirectory() const;
@ -165,6 +172,8 @@ private:
QWidget *m_pdfSettings; QWidget *m_pdfSettings;
QWidget *m_htmlSettings;
QPlainTextEdit *m_consoleEdit; QPlainTextEdit *m_consoleEdit;
QDialogButtonBox *m_btnBox; QDialogButtonBox *m_btnBox;
@ -175,7 +184,7 @@ private:
QLabel *m_layoutLabel; QLabel *m_layoutLabel;
QPushButton *m_layoutBtn; QCheckBox *m_embedStyleCB;
VNotebook *m_notebook; VNotebook *m_notebook;

View File

@ -6,6 +6,10 @@
/* BACKGROUND_PLACE_HOLDER */ /* BACKGROUND_PLACE_HOLDER */
</style> </style>
<style type="text/css">
/* STYLE_PLACE_HOLDER */
</style>
<!-- HEAD_PLACE_HOLDER --> <!-- HEAD_PLACE_HOLDER -->
</head> </head>
<body> <body>

View File

@ -40,9 +40,8 @@ if (typeof VEnableImageCaption == 'undefined') {
VEnableImageCaption = false; VEnableImageCaption = false;
} }
var headContent = function() { var styleContent = function() {
var styles = "<style type=\"text/css\">\n"; var styles = "";
for (var i = 0; i < document.styleSheets.length; ++i) { for (var i = 0; i < document.styleSheets.length; ++i) {
var styleSheet = document.styleSheets[i]; var styleSheet = document.styleSheets[i];
if (styleSheet.cssRules) { if (styleSheet.cssRules) {
@ -52,12 +51,11 @@ var headContent = function() {
} }
} }
var styles = styles + "</style>";
return styles; return styles;
}; }
var htmlContent = function() { var htmlContent = function() {
content.htmlContentCB(headContent(), placeholder.innerHTML); content.htmlContentCB("", styleContent(), placeholder.innerHTML);
}; };
new QWebChannel(qt.webChannelTransport, new QWebChannel(qt.webChannelTransport,

View File

@ -38,6 +38,7 @@ namespace HtmlHolder
static const QString c_extraHolder = "<!-- EXTRA_PLACE_HOLDER -->"; static const QString c_extraHolder = "<!-- EXTRA_PLACE_HOLDER -->";
static const QString c_bodyHolder = "<!-- BODY_PLACE_HOLDER -->"; static const QString c_bodyHolder = "<!-- BODY_PLACE_HOLDER -->";
static const QString c_headHolder = "<!-- HEAD_PLACE_HOLDER -->"; static const QString c_headHolder = "<!-- HEAD_PLACE_HOLDER -->";
static const QString c_styleHolder = "/* STYLE_PLACE_HOLDER */";
} }
// Directory Config file items. // Directory Config file items.

View File

@ -114,7 +114,9 @@ void VDocument::finishLogics()
emit logicsFinished(); emit logicsFinished();
} }
void VDocument::htmlContentCB(const QString &p_head, const QString &p_body) void VDocument::htmlContentCB(const QString &p_head,
const QString &p_style,
const QString &p_body)
{ {
emit htmlContentFinished(p_head, p_body); emit htmlContentFinished(p_head, p_style, p_body);
} }

View File

@ -70,7 +70,9 @@ public slots:
// But the page may not finish loading, such as images. // But the page may not finish loading, such as images.
void finishLogics(); void finishLogics();
void htmlContentCB(const QString &p_head, const QString &p_body); void htmlContentCB(const QString &p_head,
const QString &p_style,
const QString &p_body);
signals: signals:
void textChanged(const QString &text); void textChanged(const QString &text);
@ -103,6 +105,7 @@ signals:
void requestHtmlContent(); void requestHtmlContent();
void htmlContentFinished(const QString &p_headContent, void htmlContentFinished(const QString &p_headContent,
const QString &p_styleContent,
const QString &p_bodyContent); const QString &p_bodyContent);
private: private:

View File

@ -200,6 +200,7 @@ bool VExporter::exportViaWebView(VFile *p_file,
case ExportFormat::HTML: case ExportFormat::HTML:
exportRet = exportToHTML(m_webViewer, exportRet = exportToHTML(m_webViewer,
m_webDocument, m_webDocument,
p_opt.m_embedCssStyle,
p_outputFile); p_outputFile);
break; break;
@ -235,13 +236,16 @@ exit:
bool VExporter::exportToHTML(VWebView *p_webViewer, bool VExporter::exportToHTML(VWebView *p_webViewer,
VDocument *p_webDocument, VDocument *p_webDocument,
bool p_embedCssStyle,
const QString &p_filePath) const QString &p_filePath)
{ {
Q_UNUSED(p_webViewer); Q_UNUSED(p_webViewer);
int htmlExported = 0; int htmlExported = 0;
connect(p_webDocument, &VDocument::htmlContentFinished, connect(p_webDocument, &VDocument::htmlContentFinished,
this, [&, this](const QString &p_headContent, const QString &p_bodyContent) { this, [&, this](const QString &p_headContent,
const QString &p_styleContent,
const QString &p_bodyContent) {
if (p_bodyContent.isEmpty() || this->m_state == ExportState::Cancelled) { if (p_bodyContent.isEmpty() || this->m_state == ExportState::Cancelled) {
htmlExported = -1; htmlExported = -1;
return; return;
@ -257,7 +261,14 @@ bool VExporter::exportToHTML(VWebView *p_webViewer,
} }
QString html(m_exportHtmlTemplate); QString html(m_exportHtmlTemplate);
html.replace(HtmlHolder::c_headHolder, p_headContent); if (!p_styleContent.isEmpty() && p_embedCssStyle) {
html.replace(HtmlHolder::c_styleHolder, p_styleContent);
}
if (!p_headContent.isEmpty()) {
html.replace(HtmlHolder::c_headHolder, p_headContent);
}
html.replace(HtmlHolder::c_bodyHolder, p_bodyContent); html.replace(HtmlHolder::c_bodyHolder, p_bodyContent);
file.write(html.toUtf8()); file.write(html.toUtf8());

View File

@ -74,6 +74,7 @@ private:
bool exportToHTML(VWebView *p_webViewer, bool exportToHTML(VWebView *p_webViewer,
VDocument *p_webDocument, VDocument *p_webDocument,
bool p_embedCssStyle,
const QString &p_filePath); const QString &p_filePath);
QPageLayout m_pageLayout; QPageLayout m_pageLayout;