From 6c56380d90a3822a2be65d7c7d2537b5c7a0336f Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 27 Dec 2020 03:35:41 +0100 Subject: [PATCH] Added maddy and switched to markdown from bbcode in ListPage. --- core/utils.cpp | 18 ++ core/utils.h | 3 + libs/HEADS | 3 +- libs/maddy/LICENSE | 18 ++ libs/maddy/README.md | 82 +++++++++ libs/maddy/blockparser.h | 203 +++++++++++++++++++++ libs/maddy/breaklineparser.h | 51 ++++++ libs/maddy/checklistparser.h | 140 ++++++++++++++ libs/maddy/codeblockparser.h | 137 ++++++++++++++ libs/maddy/emphasizedparser.h | 53 ++++++ libs/maddy/headlineparser.h | 138 ++++++++++++++ libs/maddy/horizontallineparser.h | 106 +++++++++++ libs/maddy/htmlparser.h | 127 +++++++++++++ libs/maddy/imageparser.h | 53 ++++++ libs/maddy/inlinecodeparser.h | 51 ++++++ libs/maddy/italicparser.h | 50 +++++ libs/maddy/lineparser.h | 46 +++++ libs/maddy/linkparser.h | 53 ++++++ libs/maddy/orderedlistparser.h | 142 +++++++++++++++ libs/maddy/paragraphparser.h | 115 ++++++++++++ libs/maddy/parser.h | 294 ++++++++++++++++++++++++++++++ libs/maddy/parserconfig.h | 31 ++++ libs/maddy/quoteparser.h | 165 +++++++++++++++++ libs/maddy/strikethroughparser.h | 51 ++++++ libs/maddy/strongparser.h | 59 ++++++ libs/maddy/tableparser.h | 246 +++++++++++++++++++++++++ libs/maddy/unorderedlistparser.h | 133 ++++++++++++++ modules/list_page/list_page.cpp | 2 +- 28 files changed, 2568 insertions(+), 2 deletions(-) create mode 100644 libs/maddy/LICENSE create mode 100644 libs/maddy/README.md create mode 100644 libs/maddy/blockparser.h create mode 100644 libs/maddy/breaklineparser.h create mode 100644 libs/maddy/checklistparser.h create mode 100644 libs/maddy/codeblockparser.h create mode 100644 libs/maddy/emphasizedparser.h create mode 100644 libs/maddy/headlineparser.h create mode 100644 libs/maddy/horizontallineparser.h create mode 100644 libs/maddy/htmlparser.h create mode 100644 libs/maddy/imageparser.h create mode 100644 libs/maddy/inlinecodeparser.h create mode 100644 libs/maddy/italicparser.h create mode 100644 libs/maddy/lineparser.h create mode 100644 libs/maddy/linkparser.h create mode 100644 libs/maddy/orderedlistparser.h create mode 100644 libs/maddy/paragraphparser.h create mode 100644 libs/maddy/parser.h create mode 100644 libs/maddy/parserconfig.h create mode 100644 libs/maddy/quoteparser.h create mode 100644 libs/maddy/strikethroughparser.h create mode 100644 libs/maddy/strongparser.h create mode 100644 libs/maddy/tableparser.h create mode 100644 libs/maddy/unorderedlistparser.h diff --git a/core/utils.cpp b/core/utils.cpp index f8cc9a6..7832de8 100644 --- a/core/utils.cpp +++ b/core/utils.cpp @@ -1,10 +1,28 @@ #include "utils.h" +#include +#include +#include + void Utils::newline_to_br(std::string *str) { str_replace(str, "\r\n", "
"); str_replace(str, "\n", "
"); } +void Utils::markdown_to_html(std::string *str) { + std::shared_ptr config = std::make_shared(); + config->isEmphasizedParserEnabled = true; + config->isHTMLWrappedInParagraph = true; + + std::shared_ptr parser = std::make_shared(config); + + std::stringstream ss((*str)); + + std::string htmlOutput = parser->Parse(ss); + + (*str) = htmlOutput; +} + void Utils::bbcode_evaluate_simple(std::string *str) { bbcpp::BBDocumentPtr doc = bbcode((*str)); diff --git a/core/utils.h b/core/utils.h index 35b853c..23f9ea9 100644 --- a/core/utils.h +++ b/core/utils.h @@ -5,11 +5,14 @@ #include + class Utils { public: static void newline_to_br(std::string *str); //htmlspecialchars + static void markdown_to_html(std::string *str); + static void bbcode_evaluate_simple(std::string *str); static bbcpp::BBDocumentPtr bbcode(const std::string &str); diff --git a/libs/HEADS b/libs/HEADS index 4277590..21226f4 100644 --- a/libs/HEADS +++ b/libs/HEADS @@ -1,4 +1,5 @@ RapidJSON 0ccdbf364c577803e2a751f5aededce935314313 brynet b0d13e7419628d0f7051a2bb310daaf8a506e08b rapidxml 1.13 -bbcpp a035c4942ed9e5277833fe80e444406f959c3d88 \ No newline at end of file +bbcpp a035c4942ed9e5277833fe80e444406f959c3d88 +maddy adb1a910d4aadea09cb7b200f2ec204f61214596 \ No newline at end of file diff --git a/libs/maddy/LICENSE b/libs/maddy/LICENSE new file mode 100644 index 0000000..3879e0d --- /dev/null +++ b/libs/maddy/LICENSE @@ -0,0 +1,18 @@ +Copyright 2017, 2018, 2019, 2020 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 +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/libs/maddy/README.md b/libs/maddy/README.md new file mode 100644 index 0000000..e5d457f --- /dev/null +++ b/libs/maddy/README.md @@ -0,0 +1,82 @@ +# maddy + +[![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. + +## Supported OS + +It actually should work on any OS, that supports the C++14 standard library. + +It is tested to work on: + +* Linux (gcc) +* OSX (clang) +* Windows (Visual Studio 2017) + +## Dependencies + +* C++14 + +## Why maddy? + +When I was needing a Markdown parser in C++ I couldn't find any, that was +fitting my needs. So I simply wrote my own one. + +## Markdown syntax + +The supported syntax can be found in the [definitions docs](docs/definitions.md). + +## How to use + +To use maddy in your project, simply add the include path of maddy to yours +and in the code, you can then do the following: + +```c++ +#include +#include + +#include "maddy/parser.h" + +std::stringstream markdownInput(""); + +// config is optional +std::shared_ptr config = std::make_shared(); +config->isEmphasizedParserEnabled = true; // default +config->isHTMLWrappedInParagraph = true; // default + +std::shared_ptr parser = std::make_shared(config); +std::string htmlOutput = parser->Parse(markdownInput); +``` + +## How to run the tests + +*(tested on Linux with +[git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) and +[cmake](https://cmake.org/install/) installed)* + +Open your preferred terminal and type: + +```shell +git clone https://github.com/progsource/maddy.git +cd maddy +git submodule update --init --recursive +mkdir tmp +cd tmp +cmake .. +make +make test # or run the executable in ../build/MaddyTests +``` + +## How to contribute + +There are different possibilities: + +* [Create a GitHub issue](https://github.com/progsource/maddy/issues/new) +* Create a pull request with an own branch (don't forget to put yourself in the + AUTHORS file) + +Please also read [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/libs/maddy/blockparser.h b/libs/maddy/blockparser.h new file mode 100644 index 0000000..9df4fa6 --- /dev/null +++ b/libs/maddy/blockparser.h @@ -0,0 +1,203 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include +// windows compatibility includes +#include +#include + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * BlockParser + * + * The code expects every child to have the following static function to be + * implemented: + * `static bool IsStartingLine(const std::string& line)` + * + * @class + */ +class BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + BlockParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : result("", std::ios_base::ate | std::ios_base::in | std::ios_base::out) + , childParser(nullptr) + , parseLineCallback(parseLineCallback) + , getBlockParserForLineCallback(getBlockParserForLineCallback) + {} + + /** + * dtor + * + * @method + */ + virtual ~BlockParser() {} + + /** + * AddLine + * + * Adding a line which has to be parsed. + * + * @method + * @param {std::string&} line + * @return {void} + */ + virtual void + AddLine(std::string& line) + { + this->parseBlock(line); + + if (this->isInlineBlockAllowed() && !this->childParser) + { + this->childParser = this->getBlockParserForLine(line); + } + + if (this->childParser) + { + this->childParser->AddLine(line); + + if (this->childParser->IsFinished()) + { + this->result << this->childParser->GetResult().str(); + this->childParser = nullptr; + } + + return; + } + + if (this->isLineParserAllowed()) + { + this->parseLine(line); + } + + this->result << line; + } + + /** + * IsFinished + * + * Check if the BlockParser is done + * + * @method + * @return {bool} + */ + virtual bool IsFinished() const = 0; + + /** + * GetResult + * + * Get the parsed HTML output. + * + * @method + * @return {std::stringstream} + */ + std::stringstream& + GetResult() + { + return this->result; + } + + /** + * Clear + * + * Clear the result to reuse the parser object. + * + * It is only used by one test for now. + * + * @method + * @return {void} + */ + void + Clear() + { + this->result.str(""); + } + +protected: + std::stringstream result; + std::shared_ptr childParser; + + virtual bool isInlineBlockAllowed() const = 0; + virtual bool isLineParserAllowed() const = 0; + virtual void parseBlock(std::string& line) = 0; + + void + parseLine(std::string& line) + { + if (parseLineCallback) + { + parseLineCallback(line); + } + } + + uint32_t + getIndentationWidth(const std::string& line) const + { + bool hasMetNonSpace = false; + + uint32_t indentation = static_cast( + std::count_if( + line.begin(), + line.end(), + [&hasMetNonSpace](unsigned char c) + { + if (hasMetNonSpace) + { + return false; + } + + if (std::isspace(c)) + { + return true; + } + + hasMetNonSpace = true; + return false; + } + ) + ); + + return indentation; + } + + std::shared_ptr + getBlockParserForLine(const std::string& line) + { + if (getBlockParserForLineCallback) + { + return getBlockParserForLineCallback(line); + } + + return nullptr; + } + +private: + std::function parseLineCallback; + std::function(const std::string& line)> getBlockParserForLineCallback; +}; // class BlockParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/breaklineparser.h b/libs/maddy/breaklineparser.h new file mode 100644 index 0000000..e3ee6fc --- /dev/null +++ b/libs/maddy/breaklineparser.h @@ -0,0 +1,51 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/lineparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * BreakLineParser + * + * @class + */ +class BreakLineParser : public LineParser +{ +public: + /** + * Parse + * + * From Markdown: `text\r\n text` + * + * To HTML: `text
text` + * + * @method + * @param {std::string&} line The line to interpret + * @return {void} + */ + void + Parse(std::string& line) override + { + static std::regex re(R"((\r\n|\r))"); + static std::string replacement = "
"; + + line = std::regex_replace(line, re, replacement); + } +}; // class BreakLineParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/checklistparser.h b/libs/maddy/checklistparser.h new file mode 100644 index 0000000..bfc487c --- /dev/null +++ b/libs/maddy/checklistparser.h @@ -0,0 +1,140 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * ChecklistParser + * + * @class + */ +class ChecklistParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + ChecklistParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + {} + + /** + * IsStartingLine + * + * An unordered list starts with `* `. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^- \\[[x| ]\\] .*"); + return std::regex_match(line, re); + } + + /** + * IsFinished + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return this->isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return true; + } + + bool + isLineParserAllowed() const override + { + return true; + } + + void + parseBlock(std::string& line) override + { + bool isStartOfNewListItem = IsStartingLine(line); + uint32_t indentation = getIndentationWidth(line); + + static std::regex lineRegex("^(- )"); + line = std::regex_replace(line, lineRegex, ""); + + static std::regex emptyBoxRegex("^\\[ \\]"); + static std::string emptyBoxReplacement = ""; + line = std::regex_replace(line, emptyBoxRegex, emptyBoxReplacement); + + static std::regex boxRegex("^\\[x\\]"); + static std::string boxReplacement = ""; + line = std::regex_replace(line, boxRegex, boxReplacement); + + if (!this->isStarted) + { + line = "
") != std::string::npos + ) + { + line = "" + line; + this->isFinished = true; + return; + } + + if (isStartOfNewListItem) + { + line = "
  • ") != std::string::npos + ) + { + line = "" + line; + this->isFinished = true; + return; + } + + if (isStartOfNewListItem) + { + line = "
  • " + line; + } + } + +private: + bool isStarted; + bool isFinished; + + bool + isStartOfNewListItem(const std::string& line) const + { + static std::regex re("^(?:[1-9]+[0-9]*\\. |\\* ).*"); + return std::regex_match(line, re); + } +}; // class OrderedListParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/paragraphparser.h b/libs/maddy/paragraphparser.h new file mode 100644 index 0000000..303e3b0 --- /dev/null +++ b/libs/maddy/paragraphparser.h @@ -0,0 +1,115 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * ParagraphParser + * + * @class + */ +class ParagraphParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + ParagraphParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + {} + + /** + * IsStartingLine + * + * If the line is not empty, it will be a paragraph. + * + * This block parser has to always run as the last one! + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + return !line.empty(); + } + + /** + * IsFinished + * + * An empty line will end the paragraph. + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return this->isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return false; + } + + bool + isLineParserAllowed() const override + { + return true; + } + + void + parseBlock(std::string& line) override + { + if (!this->isStarted) + { + line = "

    " + line + " "; + this->isStarted = true; + return; + } + + if (line.empty()) + { + line += "

    "; + this->isFinished = true; + return; + } + + line += " "; + } + +private: + bool isStarted; + bool isFinished; +}; // class ParagraphParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/parser.h b/libs/maddy/parser.h new file mode 100644 index 0000000..add4c45 --- /dev/null +++ b/libs/maddy/parser.h @@ -0,0 +1,294 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/parserconfig.h" + +// BlockParser +#include "maddy/checklistparser.h" +#include "maddy/codeblockparser.h" +#include "maddy/headlineparser.h" +#include "maddy/horizontallineparser.h" +#include "maddy/htmlparser.h" +#include "maddy/orderedlistparser.h" +#include "maddy/paragraphparser.h" +#include "maddy/quoteparser.h" +#include "maddy/tableparser.h" +#include "maddy/unorderedlistparser.h" + +// LineParser +#include "maddy/breaklineparser.h" +#include "maddy/emphasizedparser.h" +#include "maddy/imageparser.h" +#include "maddy/inlinecodeparser.h" +#include "maddy/italicparser.h" +#include "maddy/linkparser.h" +#include "maddy/strikethroughparser.h" +#include "maddy/strongparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * Parser + * + * Transforms Markdown to HTML + * + * @class + */ +class Parser +{ +public: + /** + * ctor + * + * Initializes all `LineParser` + * + * @method + */ + Parser(std::shared_ptr config = nullptr) + : config(config) + , breakLineParser(std::make_shared()) + , emphasizedParser(std::make_shared()) + , imageParser(std::make_shared()) + , inlineCodeParser(std::make_shared()) + , italicParser(std::make_shared()) + , linkParser(std::make_shared()) + , strikeThroughParser(std::make_shared()) + , strongParser(std::make_shared()) + {} + + /** + * Parse + * + * @method + * @param {const std::istream&} markdown + * @return {std::string} HTML + */ + std::string + Parse(std::istream& markdown) const + { + std::string result = ""; + std::shared_ptr currentBlockParser = nullptr; + + for (std::string line; std::getline(markdown, line);) + { + if (!currentBlockParser) + { + currentBlockParser = getBlockParserForLine(line); + } + + if (currentBlockParser) + { + currentBlockParser->AddLine(line); + + if (currentBlockParser->IsFinished()) + { + result += currentBlockParser->GetResult().str(); + currentBlockParser = nullptr; + } + } + } + + // make sure, that all parsers are finished + if (currentBlockParser) + { + std::string emptyLine = ""; + currentBlockParser->AddLine(emptyLine); + if (currentBlockParser->IsFinished()) + { + result += currentBlockParser->GetResult().str(); + currentBlockParser = nullptr; + } + } + + return result; + } + +private: + std::shared_ptr config; + std::shared_ptr breakLineParser; + std::shared_ptr emphasizedParser; + std::shared_ptr imageParser; + std::shared_ptr inlineCodeParser; + std::shared_ptr italicParser; + std::shared_ptr linkParser; + std::shared_ptr strikeThroughParser; + std::shared_ptr strongParser; + + // block parser have to run before + void + runLineParser(std::string& line) const + { + // Attention! ImageParser has to be before LinkParser + this->imageParser->Parse(line); + this->linkParser->Parse(line); + + // Attention! StrongParser has to be before EmphasizedParser + this->strongParser->Parse(line); + + if (!this->config || this->config->isEmphasizedParserEnabled) + { + this->emphasizedParser->Parse(line); + } + + this->strikeThroughParser->Parse(line); + + this->inlineCodeParser->Parse(line); + + this->italicParser->Parse(line); + + this->breakLineParser->Parse(line); + } + + std::shared_ptr + getBlockParserForLine(const std::string& line) const + { + std::shared_ptr parser; + + if (maddy::CodeBlockParser::IsStartingLine(line)) + { + parser = std::make_shared( + nullptr, + nullptr + ); + } + else if (maddy::HeadlineParser::IsStartingLine(line)) + { + parser = std::make_shared( + nullptr, + nullptr + ); + } + else if (maddy::HorizontalLineParser::IsStartingLine(line)) + { + parser = std::make_shared( + nullptr, + nullptr + ); + } + else if (maddy::QuoteParser::IsStartingLine(line)) + { + parser = std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + [this](const std::string& line){ return this->getBlockParserForLine(line); } + ); + } + else if (maddy::TableParser::IsStartingLine(line)) + { + parser = std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + nullptr + ); + } + else if (maddy::ChecklistParser::IsStartingLine(line)) + { + parser = this->createChecklistParser(); + } + else if (maddy::OrderedListParser::IsStartingLine(line)) + { + parser = this->createOrderedListParser(); + } + else if (maddy::UnorderedListParser::IsStartingLine(line)) + { + parser = this->createUnorderedListParser(); + } + else if ( + this->config && + !this->config->isHTMLWrappedInParagraph && + maddy::HtmlParser::IsStartingLine(line) + ) + { + parser = std::make_shared(nullptr, nullptr); + } + else if (maddy::ParagraphParser::IsStartingLine(line)) + { + parser = std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + nullptr + ); + } + + return parser; + } + + std::shared_ptr + createChecklistParser() const + { + return std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + [this](const std::string& line) + { + std::shared_ptr parser; + + if (maddy::ChecklistParser::IsStartingLine(line)) + { + parser = this->createChecklistParser(); + } + + return parser; + } + ); + } + + std::shared_ptr + createOrderedListParser() const + { + return std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + [this](const std::string& line) + { + std::shared_ptr parser; + + if (maddy::OrderedListParser::IsStartingLine(line)) + { + parser = this->createOrderedListParser(); + } + else if (maddy::UnorderedListParser::IsStartingLine(line)) + { + parser = this->createUnorderedListParser(); + } + + return parser; + } + ); + } + + std::shared_ptr + createUnorderedListParser() const + { + return std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + [this](const std::string& line) + { + std::shared_ptr parser; + + if (maddy::OrderedListParser::IsStartingLine(line)) + { + parser = this->createOrderedListParser(); + } + else if (maddy::UnorderedListParser::IsStartingLine(line)) + { + parser = this->createUnorderedListParser(); + } + + return parser; + } + ); + } +}; // class Parser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/parserconfig.h b/libs/maddy/parserconfig.h new file mode 100644 index 0000000..e1b10ae --- /dev/null +++ b/libs/maddy/parserconfig.h @@ -0,0 +1,31 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * ParserConfig + * + * @class + */ +struct ParserConfig +{ + bool isEmphasizedParserEnabled; + bool isHTMLWrappedInParagraph; + + ParserConfig() + : isEmphasizedParserEnabled(true) + , isHTMLWrappedInParagraph(true) + {} +}; // class ParserConfig + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/quoteparser.h b/libs/maddy/quoteparser.h new file mode 100644 index 0000000..a3b48d0 --- /dev/null +++ b/libs/maddy/quoteparser.h @@ -0,0 +1,165 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * QuoteParser + * + * @class + */ +class QuoteParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + QuoteParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + {} + + /** + * IsStartingLine + * + * A quote starts with `> `. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^\\>.*"); + return std::regex_match(line, re); + } + + /** + * AddLine + * + * Adding a line which has to be parsed. + * + * @method + * @param {std::string&} line + * @return {void} + */ + void + AddLine(std::string& line) override + { + if (!this->isStarted) + { + this->result << "
    "; + this->isStarted = true; + } + + bool finish = false; + if (line.empty()) + { + finish = true; + } + + this->parseBlock(line); + + if (this->isInlineBlockAllowed() && !this->childParser) + { + this->childParser = this->getBlockParserForLine(line); + } + + if (this->childParser) + { + this->childParser->AddLine(line); + + if (this->childParser->IsFinished()) + { + this->result << this->childParser->GetResult().str(); + this->childParser = nullptr; + } + + return; + } + + if (this->isLineParserAllowed()) + { + this->parseLine(line); + } + + if (finish) + { + this->result << "
    "; + this->isFinished = true; + } + + this->result << line; + } + + /** + * IsFinished + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return this->isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return true; + } + + bool + isLineParserAllowed() const override + { + return true; + } + + void + parseBlock(std::string& line) override + { + static std::regex lineRegexWithSpace("^\\> "); + line = std::regex_replace(line, lineRegexWithSpace, ""); + static std::regex lineRegexWithoutSpace("^\\>"); + line = std::regex_replace(line, lineRegexWithoutSpace, ""); + + if (!line.empty()) + { + line += " "; + } + } + +private: + bool isStarted; + bool isFinished; +}; // class QuoteParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/strikethroughparser.h b/libs/maddy/strikethroughparser.h new file mode 100644 index 0000000..2640459 --- /dev/null +++ b/libs/maddy/strikethroughparser.h @@ -0,0 +1,51 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/lineparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * StrikeThroughParser + * + * @class + */ +class StrikeThroughParser : public LineParser +{ +public: + /** + * Parse + * + * From Markdown: `text ~~text~~` + * + * To HTML: `text text` + * + * @method + * @param {std::string&} line The line to interpret + * @return {void} + */ + void + Parse(std::string& line) override + { + static std::regex re("(?!.*`.*|.*.*)\\~\\~(?!.*`.*|.*<\\/code>.*)([^\\~]*)\\~\\~(?!.*`.*|.*<\\/code>.*)"); + static std::string replacement = "$1"; + + line = std::regex_replace(line, re, replacement); + } +}; // class StrikeThroughParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/strongparser.h b/libs/maddy/strongparser.h new file mode 100644 index 0000000..589dacc --- /dev/null +++ b/libs/maddy/strongparser.h @@ -0,0 +1,59 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/lineparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * StrongParser + * + * Has to be used before the `EmphasizedParser`. + * + * @class + */ +class StrongParser : public LineParser +{ +public: + /** + * Parse + * + * From Markdown: `text **text** __text__` + * + * To HTML: `text text text` + * + * @method + * @param {std::string&} line The line to interpret + * @return {void} + */ + void + Parse(std::string& line) override + { + static std::vector res + { + std::regex{"(?!.*`.*|.*.*)\\*\\*(?!.*`.*|.*<\\/code>.*)([^\\*\\*]*)\\*\\*(?!.*`.*|.*<\\/code>.*)"}, + std::regex{"(?!.*`.*|.*.*)__(?!.*`.*|.*<\\/code>.*)([^__]*)__(?!.*`.*|.*<\\/code>.*)"} + }; + static std::string replacement = "$1"; + for (const auto& re : res) + { + line = std::regex_replace(line, re, replacement); + } + } +}; // class StrongParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/tableparser.h b/libs/maddy/tableparser.h new file mode 100644 index 0000000..c230cc6 --- /dev/null +++ b/libs/maddy/tableparser.h @@ -0,0 +1,246 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * TableParser + * + * For more information, see the docs folder. + * + * @class + */ +class TableParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + TableParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + , currentBlock(0) + , currentRow(0) + {} + + /** + * IsStartingLine + * + * If the line has exact `|table>`, then it is starting the table. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::string matchString("|table>"); + return line == matchString; + } + + /** + * AddLine + * + * Adding a line which has to be parsed. + * + * @method + * @param {std::string&} line + * @return {void} + */ + void + AddLine(std::string& line) override + { + if (!this->isStarted && line == "|table>") + { + this->isStarted = true; + return; + } + + if (this->isStarted) + { + if (line == "- | - | -") + { + ++this->currentBlock; + this->currentRow = 0; + return; + } + + if (line == "|parseBlock(emptyLine); + this->isFinished = true; + return; + } + + if (this->table.size() < this->currentBlock + 1) + { + this->table.push_back(std::vector>()); + } + this->table[this->currentBlock].push_back(std::vector()); + + std::string segment; + std::stringstream streamToSplit(line); + + while (std::getline(streamToSplit, segment, '|')) + { + this->parseLine(segment); + this->table[this->currentBlock][this->currentRow].push_back(segment); + } + + ++this->currentRow; + } + } + + /** + * IsFinished + * + * A table ends with `|isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return false; + } + + bool + isLineParserAllowed() const override + { + return true; + } + + void + parseBlock(std::string&) override + { + result << ""; + + bool hasHeader = false; + bool hasFooter = false; + bool isFirstBlock = true; + uint32_t currentBlockNumber = 0; + + if (this->table.size() > 1) + { + hasHeader = true; + } + + if (this->table.size() >= 3) + { + hasFooter = true; + } + + for (const std::vector>& block : this->table) + { + bool isInHeader = false; + bool isInFooter = false; + ++currentBlockNumber; + + if (hasHeader && isFirstBlock) + { + result << ""; + isInHeader = true; + } + else if (hasFooter && currentBlockNumber == this->table.size()) + { + result << ""; + isInFooter = true; + } + else + { + result << ""; + } + + for (const std::vector& row : block) + { + result << ""; + + for (const std::string& column : row) + { + if (isInHeader) + { + result << ""; + } + } + + result << ""; + } + + if (isInHeader) + { + result << ""; + } + else if (isInFooter) + { + result << ""; + } + else + { + result << ""; + } + + isFirstBlock = false; + } + + result << "
    "; + } + else + { + result << ""; + } + + result << column; + + if (isInHeader) + { + result << ""; + } + else + { + result << "
    "; + } + +private: + bool isStarted; + bool isFinished; + uint32_t currentBlock; + uint32_t currentRow; + std::vector>> table; +}; // class TableParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/libs/maddy/unorderedlistparser.h b/libs/maddy/unorderedlistparser.h new file mode 100644 index 0000000..0c1aa5d --- /dev/null +++ b/libs/maddy/unorderedlistparser.h @@ -0,0 +1,133 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * UnorderedListParser + * + * @class + */ +class UnorderedListParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + UnorderedListParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + {} + + /** + * IsStartingLine + * + * An unordered list starts with `* `. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^[+*-] .*"); + return std::regex_match(line, re); + } + + /** + * IsFinished + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return this->isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return true; + } + + bool + isLineParserAllowed() const override + { + return true; + } + + void + parseBlock(std::string& line) override + { + bool isStartOfNewListItem = IsStartingLine(line); + uint32_t indentation = getIndentationWidth(line); + + static std::regex lineRegex("^([+*-] )"); + line = std::regex_replace(line, lineRegex, ""); + + if (!this->isStarted) + { + line = "
    • " + line; + this->isStarted = true; + return; + } + + if (indentation >= 2) + { + line = line.substr(2); + return; + } + + if ( + line.empty() || + line.find("
    • ") != std::string::npos || + line.find("
    • ") != std::string::npos || + line.find("
    ") != std::string::npos + ) + { + line = "
  • " + line; + this->isFinished = true; + return; + } + + if (isStartOfNewListItem) + { + line = "
  • " + line; + } + } + +private: + bool isStarted; + bool isFinished; +}; // class UnorderedListParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/modules/list_page/list_page.cpp b/modules/list_page/list_page.cpp index 2f607ba..93b95ff 100644 --- a/modules/list_page/list_page.cpp +++ b/modules/list_page/list_page.cpp @@ -73,7 +73,7 @@ void ListPage::load() { fclose(f); Utils::newline_to_br(&fd); - Utils::bbcode_evaluate_simple(&fd); + Utils::markdown_to_html(&fd); list_entries.push_back(fd);