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

View File

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

View File

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

View File

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

View File

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

View File

@ -114,7 +114,9 @@ void VDocument::finishLogics()
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.
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:
void textChanged(const QString &text);
@ -103,6 +105,7 @@ signals:
void requestHtmlContent();
void htmlContentFinished(const QString &p_headContent,
const QString &p_styleContent,
const QString &p_bodyContent);
private:

View File

@ -200,6 +200,7 @@ bool VExporter::exportViaWebView(VFile *p_file,
case ExportFormat::HTML:
exportRet = exportToHTML(m_webViewer,
m_webDocument,
p_opt.m_embedCssStyle,
p_outputFile);
break;
@ -235,13 +236,16 @@ exit:
bool VExporter::exportToHTML(VWebView *p_webViewer,
VDocument *p_webDocument,
bool p_embedCssStyle,
const QString &p_filePath)
{
Q_UNUSED(p_webViewer);
int htmlExported = 0;
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) {
htmlExported = -1;
return;
@ -257,7 +261,14 @@ bool VExporter::exportToHTML(VWebView *p_webViewer,
}
QString html(m_exportHtmlTemplate);
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);
file.write(html.toUtf8());

View File

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