mirror of
https://github.com/quitesimpleorg/qsmaddy.git
synced 2025-07-01 15:13:49 +02:00
Compare commits
4 Commits
fix/add_st
...
master
Author | SHA1 | Date | |
---|---|---|---|
167ce3943d | |||
e0e0db80d5 | |||
8ac353a9d7 | |||
2f14336692 |
1
AUTHORS
1
AUTHORS
@ -9,4 +9,3 @@ Patrick José Pereira (patrickelectric@gmail.com)
|
|||||||
Martin Kopecky (martin.kopecky357@gmail.com)
|
Martin Kopecky (martin.kopecky357@gmail.com)
|
||||||
Andrew Mettlach (dmmettlach@gmail.com)
|
Andrew Mettlach (dmmettlach@gmail.com)
|
||||||
Evan Klitzke (evan@eklitzke.org)
|
Evan Klitzke (evan@eklitzke.org)
|
||||||
Albert Schwarzkopf (dev-maddy@quitesimple.org)
|
|
||||||
|
83
README.md
83
README.md
@ -1,82 +1,3 @@
|
|||||||
# maddy
|
# qsmaddy
|
||||||
|
|
||||||
[](https://opensource.org/licenses/MIT)
|
Fork of [maddy](https://github.com/progsource/maddy) with some quick hacks to make it fit better for [qswiki](https://gitea.quitesimple.org/crtxcr/qswiki)
|
||||||
[](https://semver.org/)
|
|
||||||
[](https://travis-ci.org/progsource/maddy)
|
|
||||||
[](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 <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "maddy/parser.h"
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(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).
|
|
||||||
|
36
include/maddy/callbackreplacer.h
Normal file
36
include/maddy/callbackreplacer.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. For more information see the
|
||||||
|
* LICENSE file.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <regex>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace maddy {
|
||||||
|
|
||||||
|
inline std::string regex_callback_replacer(std::regex ®ex, const std::string &input,
|
||||||
|
std::function<std::string(std::smatch &)> &callback)
|
||||||
|
{
|
||||||
|
std::string result;
|
||||||
|
auto tagsbegin = std::sregex_iterator(input.begin(), input.end(), regex);
|
||||||
|
auto tagsend = std::sregex_iterator();
|
||||||
|
auto matchbegin = 0;
|
||||||
|
for(std::sregex_iterator i = tagsbegin; i != tagsend; ++i)
|
||||||
|
{
|
||||||
|
std::smatch match = *i;
|
||||||
|
|
||||||
|
auto matchlength = match.length(0);
|
||||||
|
auto matchpos = match.position();
|
||||||
|
|
||||||
|
result += input.substr(matchbegin, matchpos - matchbegin);
|
||||||
|
result += callback(match);
|
||||||
|
matchbegin = matchpos + matchlength;
|
||||||
|
}
|
||||||
|
result += input.substr(matchbegin);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include "maddy/lineparser.h"
|
#include "maddy/lineparser.h"
|
||||||
|
#include "maddy/callbackreplacer.h"
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -26,6 +27,16 @@ namespace maddy {
|
|||||||
*/
|
*/
|
||||||
class LinkParser : public LineParser
|
class LinkParser : public LineParser
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::function<std::string(std::smatch &)> callback = [](std::smatch & match){
|
||||||
|
std::string inner = match.str(1);
|
||||||
|
std::string link = match.str(2);
|
||||||
|
return "<a href=\"" + link + "\">" + inner + "</a>";
|
||||||
|
};
|
||||||
|
|
||||||
|
std::regex re = std::regex("\\[([^\\]]*)\\]\\(([^\\]]*)\\)");
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Parse
|
* Parse
|
||||||
@ -41,10 +52,12 @@ public:
|
|||||||
void
|
void
|
||||||
Parse(std::string& line) override
|
Parse(std::string& line) override
|
||||||
{
|
{
|
||||||
static std::regex re("\\[([^\\]]*)\\]\\(([^\\]]*)\\)");
|
line = regex_callback_replacer(re,line,callback);
|
||||||
static std::string replacement = "<a href=\"$2\">$1</a>";
|
}
|
||||||
|
|
||||||
line = std::regex_replace(line, re, replacement);
|
void setCallback(std::function<std::string(std::smatch &)> callback)
|
||||||
|
{
|
||||||
|
this->callback = callback;
|
||||||
}
|
}
|
||||||
}; // class LinkParser
|
}; // class LinkParser
|
||||||
|
|
||||||
|
@ -116,6 +116,54 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setBreakLineParser(std::shared_ptr<BreakLineParser> breakLineParser)
|
||||||
|
{
|
||||||
|
this->breakLineParser = breakLineParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setEmphasizedParser(std::shared_ptr<EmphasizedParser> emphasizedParser)
|
||||||
|
{
|
||||||
|
this->emphasizedParser = emphasizedParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setImageParser(std::shared_ptr<ImageParser> imageParser)
|
||||||
|
{
|
||||||
|
this->imageParser = imageParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setInlineCodeParser(std::shared_ptr<InlineCodeParser> inlineCodeParser)
|
||||||
|
{
|
||||||
|
this->inlineCodeParser = inlineCodeParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setItalicParser(std::shared_ptr<ItalicParser> italicParser)
|
||||||
|
{
|
||||||
|
this->italicParser = italicParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setLinkParser(std::shared_ptr<LinkParser> linkParser)
|
||||||
|
{
|
||||||
|
this->linkParser = linkParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setStrikeThroughParser(std::shared_ptr<StrikeThroughParser> strikeThroughParser)
|
||||||
|
{
|
||||||
|
this->strikeThroughParser = strikeThroughParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setStrongParser(std::shared_ptr<StrongParser> strongParser)
|
||||||
|
{
|
||||||
|
this->strongParser = strongParser;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<ParserConfig> config;
|
std::shared_ptr<ParserConfig> config;
|
||||||
std::shared_ptr<BreakLineParser> breakLineParser;
|
std::shared_ptr<BreakLineParser> breakLineParser;
|
||||||
|
Reference in New Issue
Block a user