feat: add config options to en-/disable each parser

Also fix cmake line, so that ctest can find the test executable;
update readme.
This commit is contained in:
Petra Baranski
2023-07-23 09:43:32 +02:00
parent 51e1813373
commit 41396ab246
9 changed files with 310 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
Copyright 2017, 2018, 2019, 2020 M. Petra Baranski
Copyright 2017, 2018, 2019, 2020, 2023 M. Petra Baranski
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in

View File

@@ -2,8 +2,6 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Version: 1.1.2](https://img.shields.io/badge/Version-1.1.2-brightgreen.svg)](https://semver.org/)
[![Travis Build Status](https://travis-ci.org/progsource/maddy.svg?branch=master)](https://travis-ci.org/progsource/maddy)
[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/04m0lg27kigv1pg8/branch/master?svg=true)](https://ci.appveyor.com/project/progsource/maddy/branch/master)
maddy is a C++ Markdown to HTML **header-only** parser library.
@@ -45,13 +43,17 @@ std::stringstream markdownInput("");
// config is optional
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
config->isEmphasizedParserEnabled = true; // default
config->isHTMLWrappedInParagraph = true; // default
// config->isEmphasizedParserEnabled = false; // default true - this flag is deprecated
// config->isHTMLWrappedInParagraph = false; // default true - this flag is deprecated
config->enabledParsers &= ~maddy::types::EMPHASIZED_PARSER;
config->enabledParsers |= maddy::types::HTML_PARSER;
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(config);
std::string htmlOutput = parser->Parse(markdownInput);
```
You can find all parser flags in [`include/maddy/parserconfig.h`](include/maddy/parserconfig.h).
## How to run the tests
*(tested on Linux with

View File

@@ -34,11 +34,13 @@ public:
*/
ParagraphParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback,
bool isEnabled
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
, isFinished(false)
, isEnabled(isEnabled)
{}
/**
@@ -88,19 +90,31 @@ protected:
void
parseBlock(std::string& line) override
{
if (!this->isStarted)
if (this->isEnabled && !this->isStarted)
{
line = "<p>" + line + " ";
this->isStarted = true;
return;
}
else if (!this->isEnabled && !this->isStarted)
{
line += " ";
this->isStarted = true;
return;
}
if (line.empty())
if (this->isEnabled && line.empty())
{
line += "</p>";
this->isFinished = true;
return;
}
else if (!this->isEnabled && line.empty())
{
line += "<br/>";
this->isFinished = true;
return;
}
line += " ";
}
@@ -108,6 +122,7 @@ protected:
private:
bool isStarted;
bool isFinished;
bool isEnabled;
}; // class ParagraphParser
// -----------------------------------------------------------------------------

View File

@@ -59,15 +59,82 @@ public:
*/
Parser(std::shared_ptr<ParserConfig> config = nullptr)
: config(config)
, breakLineParser(std::make_shared<BreakLineParser>())
, emphasizedParser(std::make_shared<EmphasizedParser>())
, imageParser(std::make_shared<ImageParser>())
, inlineCodeParser(std::make_shared<InlineCodeParser>())
, italicParser(std::make_shared<ItalicParser>())
, linkParser(std::make_shared<LinkParser>())
, strikeThroughParser(std::make_shared<StrikeThroughParser>())
, strongParser(std::make_shared<StrongParser>())
{}
{
// deprecated backward compatibility
// will be removed in 1.4.0 latest including the booleans
if (this->config && !this->config->isEmphasizedParserEnabled)
{
this->config->enabledParsers &= ~maddy::types::EMPHASIZED_PARSER;
}
if (this->config && !this->config->isHTMLWrappedInParagraph)
{
this->config->enabledParsers |= maddy::types::HTML_PARSER;
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::BREAKLINE_PARSER) != 0
)
{
this->breakLineParser = std::make_shared<BreakLineParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::EMPHASIZED_PARSER) != 0
)
{
this->emphasizedParser = std::make_shared<EmphasizedParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::IMAGE_PARSER) != 0
)
{
this->imageParser = std::make_shared<ImageParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::INLINE_CODE_PARSER) != 0
)
{
this->inlineCodeParser = std::make_shared<InlineCodeParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::ITALIC_PARSER) != 0
)
{
this->italicParser = std::make_shared<ItalicParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::LINK_PARSER) != 0
)
{
this->linkParser = std::make_shared<LinkParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::STRIKETHROUGH_PARSER) != 0
)
{
this->strikeThroughParser = std::make_shared<StrikeThroughParser>();
}
if (
!this->config ||
(this->config->enabledParsers & maddy::types::STRONG_PARSER) != 0
)
{
this->strongParser = std::make_shared<StrongParser>();
}
}
/**
* Parse
@@ -132,24 +199,46 @@ private:
runLineParser(std::string& line) const
{
// Attention! ImageParser has to be before LinkParser
this->imageParser->Parse(line);
this->linkParser->Parse(line);
if (this->imageParser)
{
this->imageParser->Parse(line);
}
if (this->linkParser)
{
this->linkParser->Parse(line);
}
// Attention! StrongParser has to be before EmphasizedParser
this->strongParser->Parse(line);
if (this->strongParser)
{
this->strongParser->Parse(line);
}
if (!this->config || this->config->isEmphasizedParserEnabled)
if (this->emphasizedParser)
{
this->emphasizedParser->Parse(line);
}
this->strikeThroughParser->Parse(line);
if (this->strikeThroughParser)
{
this->strikeThroughParser->Parse(line);
}
this->inlineCodeParser->Parse(line);
if (this->inlineCodeParser)
{
this->inlineCodeParser->Parse(line);
}
this->italicParser->Parse(line);
if (this->italicParser)
{
this->italicParser->Parse(line);
}
this->breakLineParser->Parse(line);
if (this->breakLineParser)
{
this->breakLineParser->Parse(line);
}
}
std::shared_ptr<BlockParser>
@@ -157,66 +246,117 @@ private:
{
std::shared_ptr<BlockParser> parser;
if (maddy::CodeBlockParser::IsStartingLine(line))
if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::CODE_BLOCK_PARSER) != 0
) &&
maddy::CodeBlockParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::CodeBlockParser>(
nullptr,
nullptr
);
}
else if (maddy::HeadlineParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::HEADLINE_PARSER) != 0
) &&
maddy::HeadlineParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::HeadlineParser>(
nullptr,
nullptr
);
}
else if (maddy::HorizontalLineParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::HORIZONTAL_LINE_PARSER) != 0
) &&
maddy::HorizontalLineParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::HorizontalLineParser>(
nullptr,
nullptr
);
}
else if (maddy::QuoteParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::QUOTE_PARSER) != 0
) &&
maddy::QuoteParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::QuoteParser>(
[this](std::string& line){ this->runLineParser(line); },
[this](const std::string& line){ return this->getBlockParserForLine(line); }
);
}
else if (maddy::TableParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::TABLE_PARSER) != 0
) &&
maddy::TableParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::TableParser>(
[this](std::string& line){ this->runLineParser(line); },
nullptr
);
}
else if (maddy::ChecklistParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::CHECKLIST_PARSER) != 0
) &&
maddy::ChecklistParser::IsStartingLine(line)
)
{
parser = this->createChecklistParser();
}
else if (maddy::OrderedListParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0
) &&
maddy::OrderedListParser::IsStartingLine(line)
)
{
parser = this->createOrderedListParser();
}
else if (maddy::UnorderedListParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0
) &&
maddy::UnorderedListParser::IsStartingLine(line)
)
{
parser = this->createUnorderedListParser();
}
else if (
this->config &&
!this->config->isHTMLWrappedInParagraph &&
(this->config->enabledParsers & maddy::types::HTML_PARSER) != 0 &&
maddy::HtmlParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::HtmlParser>(nullptr, nullptr);
}
else if (maddy::ParagraphParser::IsStartingLine(line))
else if (
maddy::ParagraphParser::IsStartingLine(line)
)
{
parser = std::make_shared<maddy::ParagraphParser>(
[this](std::string& line){ this->runLineParser(line); },
nullptr
nullptr,
(!this->config || (this->config->enabledParsers & maddy::types::PARAGRAPH_PARSER) != 0)
);
}
@@ -232,7 +372,13 @@ private:
{
std::shared_ptr<BlockParser> parser;
if (maddy::ChecklistParser::IsStartingLine(line))
if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::CHECKLIST_PARSER) != 0
) &&
maddy::ChecklistParser::IsStartingLine(line)
)
{
parser = this->createChecklistParser();
}
@@ -251,11 +397,23 @@ private:
{
std::shared_ptr<BlockParser> parser;
if (maddy::OrderedListParser::IsStartingLine(line))
if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0
) &&
maddy::OrderedListParser::IsStartingLine(line)
)
{
parser = this->createOrderedListParser();
}
else if (maddy::UnorderedListParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0
) &&
maddy::UnorderedListParser::IsStartingLine(line)
)
{
parser = this->createUnorderedListParser();
}
@@ -274,11 +432,23 @@ private:
{
std::shared_ptr<BlockParser> parser;
if (maddy::OrderedListParser::IsStartingLine(line))
if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0
) &&
maddy::OrderedListParser::IsStartingLine(line)
)
{
parser = this->createOrderedListParser();
}
else if (maddy::UnorderedListParser::IsStartingLine(line))
else if (
(
!this->config ||
(this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0
) &&
maddy::UnorderedListParser::IsStartingLine(line)
)
{
parser = this->createUnorderedListParser();
}

View File

@@ -4,12 +4,50 @@
*/
#pragma once
#include <stdint.h>
// -----------------------------------------------------------------------------
namespace maddy {
// -----------------------------------------------------------------------------
namespace types {
/**
* PARSER_TYPE
*
* Bitwise flags to turn on/off each parser
*/
enum PARSER_TYPE : uint32_t
{
NONE = 0,
BREAKLINE_PARSER = 0b1,
CHECKLIST_PARSER = 0b10,
CODE_BLOCK_PARSER = 0b100,
EMPHASIZED_PARSER = 0b1000,
HEADLINE_PARSER = 0b10000,
HORIZONTAL_LINE_PARSER = 0b100000,
HTML_PARSER = 0b1000000,
IMAGE_PARSER = 0b10000000,
INLINE_CODE_PARSER = 0b100000000,
ITALIC_PARSER = 0b1000000000,
LINK_PARSER = 0b10000000000,
ORDERED_LIST_PARSER = 0b100000000000,
PARAGRAPH_PARSER = 0b1000000000000,
QUOTE_PARSER = 0b10000000000000,
STRIKETHROUGH_PARSER = 0b100000000000000,
STRONG_PARSER = 0b1000000000000000,
TABLE_PARSER = 0b10000000000000000,
UNORDERED_LIST_PARSER = 0b100000000000000000,
DEFAULT = 0b111111111110111111,
ALL = 0b111111111111111111,
};
} // namespace types
/**
* ParserConfig
*
@@ -17,12 +55,22 @@ namespace maddy {
*/
struct ParserConfig
{
/**
* @deprecated will be removed in 1.4.0 latest
*/
bool isEmphasizedParserEnabled;
/**
* @deprecated will be removed in 1.4.0 latest
*/
bool isHTMLWrappedInParagraph;
uint32_t enabledParsers;
ParserConfig()
: isEmphasizedParserEnabled(true)
, isHTMLWrappedInParagraph(true)
, enabledParsers(maddy::types::DEFAULT)
{}
}; // class ParserConfig

View File

@@ -25,4 +25,4 @@ target_include_directories(MaddyTests PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(MaddyTests maddy gmock_main)
add_test(MaddyTests MaddyTests)
add_test(NAME MaddyTests COMMAND MaddyTests)

View File

@@ -20,7 +20,8 @@ protected:
{
this->pParser = std::make_shared<maddy::ParagraphParser>(
nullptr,
nullptr
nullptr,
true
);
}
};

View File

@@ -34,3 +34,33 @@ TEST(MADDY_PARSER, ItShouldParseWithConfig)
ASSERT_EQ(testHtml2, output);
}
TEST(MADDY_PARSER, ItShouldParseWithBitwiseConfig)
{
auto config = std::make_shared<maddy::ParserConfig>();
config->enabledParsers &= ~maddy::types::EMPHASIZED_PARSER;
config->enabledParsers |= maddy::types::HTML_PARSER;
auto parser = std::make_shared<maddy::Parser>(config);
std::stringstream markdown(testMarkdown);
const std::string output = parser->Parse(markdown);
ASSERT_EQ(testHtml2, output);
}
TEST(MADDY_PARSER, ItShouldParseWithSmallConfig)
{
auto config = std::make_shared<maddy::ParserConfig>();
config->enabledParsers = maddy::types::EMPHASIZED_PARSER |
maddy::types::STRONG_PARSER;
auto parser = std::make_shared<maddy::Parser>(config);
std::stringstream markdown(testMarkdown);
const std::string output = parser->Parse(markdown);
ASSERT_EQ(testHtml3, output);
}

View File

@@ -71,3 +71,4 @@ foot a|foot b|foot c\n\
const std::string testHtml = "<h1>This is a test</h1><p>This should result in a praragraph it's that simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some <strong>hierarchy</strong><ol><li>and an <em>ordered</em></li><li>list</li><li>directly</li></ol></li><li>inside</li></ul></li></ul><pre><code>\nvar c = 'blub';\n</code></pre><blockquote><p>A Quote </p><p>With some <s>text</s> blocks inside </p><ul><li>even a list </li><li>should be </li><li>possible </li></ul></blockquote><p>And well <code>inline code</code> should also work. </p><h2>Another Headline</h2><p>And not to forget <a href=\"http://progsource.de\">link to progsource</a> should work. And well - let's see how an image would be shown: </p><p><img src=\"http://progsource.de/img/progsource.png\" alt=\"an image\"/> </p><hr/><p><a name=\"to top\"></a> </p><h3>and more headlines</h3><ul class=\"checklist\"><li><label><input type=\"checkbox\"/> how</label></li><li><label><input type=\"checkbox\"/> about<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> nice</label></li></ul></label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> check</label></li><li><label><input type=\"checkbox\"/> list</label></li></ul><h4>even a table</h4><table><thead><tr><th>Left header</th><th>middle header</th><th>last header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml2 = "<h1>This is a test</h1><p>This should result in a praragraph it's that simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some <strong>hierarchy</strong><ol><li>and an _ordered_</li><li>list</li><li>directly</li></ol></li><li>inside</li></ul></li></ul><pre><code>\nvar c = 'blub';\n</code></pre><blockquote><p>A Quote </p><p>With some <s>text</s> blocks inside </p><ul><li>even a list </li><li>should be </li><li>possible </li></ul></blockquote><p>And well <code>inline code</code> should also work. </p><h2>Another Headline</h2><p>And not to forget <a href=\"http://progsource.de\">link to progsource</a> should work. And well - let's see how an image would be shown: </p><p><img src=\"http://progsource.de/img/progsource.png\" alt=\"an image\"/> </p><hr/><a name=\"to top\"></a><h3>and more headlines</h3><ul class=\"checklist\"><li><label><input type=\"checkbox\"/> how</label></li><li><label><input type=\"checkbox\"/> about<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> nice</label></li></ul></label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> check</label></li><li><label><input type=\"checkbox\"/> list</label></li></ul><h4>even a table</h4><table><thead><tr><th>Left header</th><th>middle header</th><th>last header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml3 = "# This is a test <br/>This should result in a praragraph it's that simple. <br/>* an *unordered* list * with some <strong>hierarchy</strong> 1. and an <em>ordered</em> * list * directly * inside <br/>``` var c = 'blub'; ``` <br/>> A Quote > > With some ~~text~~ blocks inside > > * even a list > * should be > * possible > <br/>And well `inline code` should also work. <br/>## Another Headline <br/>And not to forget [link to progsource](http://progsource.de) should work. And well - let's see how an image would be shown: <br/>![an image](http://progsource.de/img/progsource.png) <br/>--- <br/><a name=\"to top\"></a> <br/>### and more headlines <br/>- [ ] how - [ ] about - [ ] a - [x] nice - [x] check - [ ] list <br/>#### even a table <br/>|table> Left header|middle header|last header - | - | - cell 1|cell <strong>2</strong>|cell 3 cell 4|cell 5|cell 6 - | - | - foot a|foot b|foot c |<table <br/>##### h5 ###### h6 <br/>";