MarkDown: Enable option to disable smartypants formatting

BUGS: 404616

Subscribers: okular-devel

Tags: #okular

Differential Revision: https://phabricator.kde.org/D19405
remotes/origin/gsoc2019_animatedpdf
Albert Astals Cid 7 years ago
parent 35b95b36aa
commit fe0c8ad7c9
  1. 20
      core/textdocumentgenerator.cpp
  2. 6
      core/textdocumentgenerator.h
  3. 51
      generators/markdown/converter.cpp
  4. 14
      generators/markdown/converter.h
  5. 32
      generators/markdown/generator_md.cpp
  6. 7
      generators/markdown/generator_md.h

@ -23,6 +23,7 @@
#include "action.h" #include "action.h"
#include "annotations.h" #include "annotations.h"
#include "document_p.h"
#include "page.h" #include "page.h"
#include "textpage.h" #include "textpage.h"
@ -563,5 +564,24 @@ TextDocumentSettings* TextDocumentGenerator::generalSettings()
return d->mGeneralSettings; return d->mGeneralSettings;
} }
TextDocumentConverter* TextDocumentGenerator::converter()
{
Q_D( TextDocumentGenerator );
return d->mConverter;
}
void TextDocumentGenerator::setTextDocument( QTextDocument *textDocument )
{
Q_D( TextDocumentGenerator );
d->mDocument = textDocument;
Q_FOREACH (Page *p, d->m_document->m_pagesVector )
{
p->setTextPage( nullptr );
}
}
#include "moc_textdocumentgenerator.cpp" #include "moc_textdocumentgenerator.cpp"

@ -213,6 +213,12 @@ class OKULARCORE_EXPORT TextDocumentGenerator : public Generator, public Okular:
bool doCloseDocument() override; bool doCloseDocument() override;
Okular::TextPage* textPage( Okular::TextRequest *request ) override; Okular::TextPage* textPage( Okular::TextRequest *request ) override;
/* @since 1.8 */
TextDocumentConverter* converter();
/* @since 1.8 */
void setTextDocument( QTextDocument *textDocument );
private: private:
Q_DECLARE_PRIVATE( TextDocumentGenerator ) Q_DECLARE_PRIVATE( TextDocumentGenerator )
Q_DISABLE_COPY( TextDocumentGenerator ) Q_DISABLE_COPY( TextDocumentGenerator )

@ -9,6 +9,8 @@
#include "converter.h" #include "converter.h"
#include "generator_md.h"
#include <KLocalizedString> #include <KLocalizedString>
#include <QDir> #include <QDir>
@ -38,37 +40,59 @@ extern "C" {
#ifndef MKD_AUTOLINK #ifndef MKD_AUTOLINK
#define MKD_AUTOLINK 0 #define MKD_AUTOLINK 0
#endif #endif
using namespace Markdown; using namespace Markdown;
Converter::Converter() Converter::Converter()
: m_markdownFile( nullptr )
{ {
} }
Converter::~Converter() Converter::~Converter()
{ {
if ( m_markdownFile )
{
fclose( m_markdownFile );
}
} }
QTextDocument* Converter::convert( const QString &fileName ) QTextDocument* Converter::convert( const QString &fileName )
{ {
FILE *markdownFile = fopen( fileName.toLocal8Bit(), "rb" ); m_markdownFile = fopen( fileName.toLocal8Bit(), "rb" );
if ( !markdownFile ) { if ( !m_markdownFile ) {
emit error( i18n( "Failed to open the document" ), -1 ); emit error( i18n( "Failed to open the document" ), -1 );
return nullptr; return nullptr;
} }
const QDir dir = QDir( fileName.left( fileName.lastIndexOf( '/' ) ) ); m_fileDir = QDir( fileName.left( fileName.lastIndexOf( '/' ) ) );
MMIOT *markdownHandle = mkd_in( markdownFile, 0 ); QTextDocument *doc = convertOpenFile();
extractLinks( doc->rootFrame() );
return doc;
}
void Converter::convertAgain()
{
setDocument( convertOpenFile() );
}
QTextDocument *Converter::convertOpenFile()
{
rewind( m_markdownFile );
MMIOT *markdownHandle = mkd_in( m_markdownFile, 0 );
if ( !mkd_compile( markdownHandle, MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK ) ) { int flags = MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK;
if (!MarkdownGenerator::isFancyPantsEnabled())
flags |= MKD_NOPANTS;
if ( !mkd_compile( markdownHandle, flags ) ) {
emit error( i18n( "Failed to compile the Markdown document." ), -1 ); emit error( i18n( "Failed to compile the Markdown document." ), -1 );
return 0; return 0;
} }
char *htmlDocument; char *htmlDocument;
const int size = mkd_document( markdownHandle, &htmlDocument ); const int size = mkd_document( markdownHandle, &htmlDocument );
const QString html = QString::fromUtf8( htmlDocument, size ); const QString html = QString::fromUtf8( htmlDocument, size );
QTextDocument *textDocument = new QTextDocument; QTextDocument *textDocument = new QTextDocument;
@ -84,27 +108,26 @@ QTextDocument* Converter::convert( const QString &fileName )
QTextFrame *rootFrame = textDocument->rootFrame(); QTextFrame *rootFrame = textDocument->rootFrame();
rootFrame->setFrameFormat( frameFormat ); rootFrame->setFrameFormat( frameFormat );
convertLinks( rootFrame ); convertImages( rootFrame, m_fileDir, textDocument );
convertImages( rootFrame, dir, textDocument );
return textDocument; return textDocument;
} }
void Converter::convertLinks(QTextFrame * parent) void Converter::extractLinks(QTextFrame * parent)
{ {
for ( QTextFrame::iterator it = parent->begin(); !it.atEnd(); ++it ) { for ( QTextFrame::iterator it = parent->begin(); !it.atEnd(); ++it ) {
QTextFrame *textFrame = it.currentFrame(); QTextFrame *textFrame = it.currentFrame();
const QTextBlock textBlock = it.currentBlock(); const QTextBlock textBlock = it.currentBlock();
if ( textFrame ) { if ( textFrame ) {
convertLinks(textFrame); extractLinks(textFrame);
} else if ( textBlock.isValid() ) { } else if ( textBlock.isValid() ) {
convertLinks(textBlock); extractLinks(textBlock);
} }
} }
} }
void Converter::convertLinks(const QTextBlock & parent) void Converter::extractLinks(const QTextBlock & parent)
{ {
for ( QTextBlock::iterator it = parent.begin(); !it.atEnd(); ++it ) { for ( QTextBlock::iterator it = parent.begin(); !it.atEnd(); ++it ) {
const QTextFragment textFragment = it.fragment(); const QTextFragment textFragment = it.fragment();

@ -12,7 +12,8 @@
#include <core/textdocumentgenerator.h> #include <core/textdocumentgenerator.h>
class QDir; #include <QDir>
class QTextBlock; class QTextBlock;
class QTextFrame; class QTextFrame;
@ -28,11 +29,18 @@ class Converter : public Okular::TextDocumentConverter
QTextDocument *convert( const QString &fileName ) override; QTextDocument *convert( const QString &fileName ) override;
void convertAgain();
QTextDocument *convertOpenFile();
private: private:
void convertLinks(QTextFrame *parent); void extractLinks(QTextFrame *parent);
void convertLinks(const QTextBlock& parent); void extractLinks(const QTextBlock& parent);
void convertImages(QTextFrame *parent, const QDir &dir, QTextDocument *textDocument); void convertImages(QTextFrame *parent, const QDir &dir, QTextDocument *textDocument);
void convertImages(const QTextBlock& parent, const QDir &dir, QTextDocument *textDocument); void convertImages(const QTextBlock& parent, const QDir &dir, QTextDocument *textDocument);
FILE *m_markdownFile;
QDir m_fileDir;
}; };
} }

@ -11,22 +11,54 @@
#include "converter.h" #include "converter.h"
#include "debug_md.h" #include "debug_md.h"
#include "mdsettings.h"
#include <KAboutData> #include <KAboutData>
#include <KLocalizedString> #include <KLocalizedString>
#include <kconfigdialog.h> #include <kconfigdialog.h>
#include <QCheckBox>
OKULAR_EXPORT_PLUGIN(MarkdownGenerator, "libokularGenerator_md.json") OKULAR_EXPORT_PLUGIN(MarkdownGenerator, "libokularGenerator_md.json")
bool MarkdownGenerator::s_isFancyPantsEnabled = true;
bool MarkdownGenerator::s_wasFancyPantsEnabled = true;
MarkdownGenerator::MarkdownGenerator( QObject *parent, const QVariantList &args ) MarkdownGenerator::MarkdownGenerator( QObject *parent, const QVariantList &args )
: Okular::TextDocumentGenerator( new Markdown::Converter, QStringLiteral("okular_markdown_generator_settings"), parent, args ) : Okular::TextDocumentGenerator( new Markdown::Converter, QStringLiteral("okular_markdown_generator_settings"), parent, args )
{ {
Okular::TextDocumentSettings *mdSettings = generalSettings();
mdSettings->addItemBool( "SmartyPants", s_isFancyPantsEnabled, true );
mdSettings->load();
s_wasFancyPantsEnabled = s_isFancyPantsEnabled;
}
bool MarkdownGenerator::reparseConfig()
{
const bool textDocumentGeneratorChangedConfig = Okular::TextDocumentGenerator::reparseConfig();
if (s_wasFancyPantsEnabled != s_isFancyPantsEnabled) {
s_wasFancyPantsEnabled = s_isFancyPantsEnabled;
Markdown::Converter *c = static_cast<Markdown::Converter*>( converter() );
c->convertAgain();
setTextDocument( c->document() );
return true;
}
return textDocumentGeneratorChangedConfig;
} }
void MarkdownGenerator::addPages( KConfigDialog* dlg ) void MarkdownGenerator::addPages( KConfigDialog* dlg )
{ {
Okular::TextDocumentSettingsWidget *widget = new Okular::TextDocumentSettingsWidget(); Okular::TextDocumentSettingsWidget *widget = new Okular::TextDocumentSettingsWidget();
QCheckBox *enableSmartyPants = new QCheckBox( dlg );
enableSmartyPants->setObjectName( QString::fromUtf8( "kcfg_SmartyPants" ) );
widget->addRow( i18n("Enable SmartyPants formatting"), enableSmartyPants );
dlg->addPage( widget, generalSettings(), i18n("Markdown"), QStringLiteral("text-markdown"), i18n("Markdown Backend Configuration") ); dlg->addPage( widget, generalSettings(), i18n("Markdown"), QStringLiteral("text-markdown"), i18n("Markdown Backend Configuration") );
} }

@ -21,7 +21,14 @@ class MarkdownGenerator : public Okular::TextDocumentGenerator
MarkdownGenerator( QObject *parent, const QVariantList &args ); MarkdownGenerator( QObject *parent, const QVariantList &args );
// [INHERITED] reparse configuration // [INHERITED] reparse configuration
bool reparseConfig() override;
void addPages( KConfigDialog* dlg ) override; void addPages( KConfigDialog* dlg ) override;
static bool isFancyPantsEnabled() { return s_isFancyPantsEnabled; }
private:
static bool s_isFancyPantsEnabled;
static bool s_wasFancyPantsEnabled;
}; };
#endif #endif

Loading…
Cancel
Save