mirror of
https://github.com/quitesimpleorg/qsmaddy.git
synced 2025-06-26 21:22:05 +02:00
No paragraphs for html (#27)
* htmlparser added * Added ParserConfig * added option to disable the emphasized parser * added option to not wrap HTML in markdown within a paragraph in output * Updated docs * Version update 1.1.1
This commit is contained in:
127
include/maddy/htmlparser.h
Normal file
127
include/maddy/htmlparser.h
Normal file
@ -0,0 +1,127 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. For more information see the
|
||||
* LICENSE file.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "maddy/blockparser.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace maddy {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* HtmlParser
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class HtmlParser : public BlockParser
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* ctor
|
||||
*
|
||||
* @method
|
||||
* @param {std::function<void(std::string&)>} parseLineCallback
|
||||
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
|
||||
*/
|
||||
HtmlParser(
|
||||
std::function<void(std::string&)> parseLineCallback,
|
||||
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
|
||||
)
|
||||
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
|
||||
, isStarted(false)
|
||||
, isFinished(false)
|
||||
, isGreaterThanFound(false)
|
||||
{}
|
||||
|
||||
/**
|
||||
* IsStartingLine
|
||||
*
|
||||
* If the line is starting with `<`, HTML is expected to follow.
|
||||
* Nothing after that will be parsed, it only is copied.
|
||||
*
|
||||
* @method
|
||||
* @param {const std::string&} line
|
||||
* @return {bool}
|
||||
*/
|
||||
static bool
|
||||
IsStartingLine(const std::string& line)
|
||||
{
|
||||
return line[0] == '<';
|
||||
}
|
||||
|
||||
/**
|
||||
* IsFinished
|
||||
*
|
||||
* `>` followed by an empty line will end the HTML block.
|
||||
*
|
||||
* @method
|
||||
* @return {bool}
|
||||
*/
|
||||
bool
|
||||
IsFinished() const override
|
||||
{
|
||||
return this->isFinished;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool
|
||||
isInlineBlockAllowed() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
isLineParserAllowed() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
parseBlock(std::string& line) override
|
||||
{
|
||||
if (!this->isStarted)
|
||||
{
|
||||
this->isStarted = true;
|
||||
}
|
||||
|
||||
if (!line.empty() && line[line.size() - 1] == '>')
|
||||
{
|
||||
this->isGreaterThanFound = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (line.empty() && this->isGreaterThanFound)
|
||||
{
|
||||
this->isFinished = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!line.empty() && this->isGreaterThanFound)
|
||||
{
|
||||
this->isGreaterThanFound = false;
|
||||
}
|
||||
|
||||
if (!line.empty())
|
||||
{
|
||||
line += " ";
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool isStarted;
|
||||
bool isFinished;
|
||||
bool isGreaterThanFound;
|
||||
}; // class HtmlParser
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
} // namespace maddy
|
@ -44,8 +44,9 @@ public:
|
||||
/**
|
||||
* IsStartingLine
|
||||
*
|
||||
* If the line is not empty, it will be a paragraph. So this block parser has
|
||||
* to always run as the last one!
|
||||
* 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
|
||||
|
@ -10,11 +10,14 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#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"
|
||||
@ -54,8 +57,9 @@ public:
|
||||
*
|
||||
* @method
|
||||
*/
|
||||
Parser()
|
||||
: breakLineParser(std::make_shared<BreakLineParser>())
|
||||
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>())
|
||||
@ -113,6 +117,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<ParserConfig> config;
|
||||
std::shared_ptr<BreakLineParser> breakLineParser;
|
||||
std::shared_ptr<EmphasizedParser> emphasizedParser;
|
||||
std::shared_ptr<ImageParser> imageParser;
|
||||
@ -132,7 +137,11 @@ private:
|
||||
|
||||
// Attention! StrongParser has to be before EmphasizedParser
|
||||
this->strongParser->Parse(line);
|
||||
this->emphasizedParser->Parse(line);
|
||||
|
||||
if (!this->config || this->config->isEmphasizedParserEnabled)
|
||||
{
|
||||
this->emphasizedParser->Parse(line);
|
||||
}
|
||||
|
||||
this->strikeThroughParser->Parse(line);
|
||||
|
||||
@ -195,6 +204,14 @@ private:
|
||||
{
|
||||
parser = this->createUnorderedListParser();
|
||||
}
|
||||
else if (
|
||||
this->config &&
|
||||
!this->config->isHTMLWrappedInParagraph &&
|
||||
maddy::HtmlParser::IsStartingLine(line)
|
||||
)
|
||||
{
|
||||
parser = std::make_shared<maddy::HtmlParser>(nullptr, nullptr);
|
||||
}
|
||||
else if (maddy::ParagraphParser::IsStartingLine(line))
|
||||
{
|
||||
parser = std::make_shared<maddy::ParagraphParser>(
|
||||
|
31
include/maddy/parserconfig.h
Normal file
31
include/maddy/parserconfig.h
Normal file
@ -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
|
Reference in New Issue
Block a user