mirror of
https://github.com/quitesimpleorg/qsmaddy.git
synced 2024-12-25 04:29:48 +01:00
Merge pull request #10 from patrickelectric/add_italic
Add italic parser
This commit is contained in:
commit
be681034e0
@ -229,10 +229,20 @@ results in
|
||||
<strong>bold text</strong>
|
||||
```
|
||||
|
||||
## italic
|
||||
|
||||
```
|
||||
*italic text*
|
||||
```
|
||||
results in
|
||||
```html
|
||||
<i>italic text</i>
|
||||
```
|
||||
|
||||
## emphasized
|
||||
|
||||
```
|
||||
*emphasized text*
|
||||
_emphasized text_
|
||||
```
|
||||
results in
|
||||
```html
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
/**
|
||||
* Parse
|
||||
*
|
||||
* From Markdown: `text *text*`
|
||||
* From Markdown: `text _text_`
|
||||
*
|
||||
* To HTML: `text <em>text</em>`
|
||||
*
|
||||
@ -41,7 +41,7 @@ public:
|
||||
void
|
||||
Parse(std::string& line) override
|
||||
{
|
||||
static std::regex re("(?!.*`.*|.*<code>.*)\\*(?!.*`.*|.*<\\/code>.*)([^\\*]*)\\*(?!.*`.*|.*<\\/code>.*)");
|
||||
static std::regex re("(?!.*`.*|.*<code>.*)\\_(?!.*`.*|.*<\\/code>.*)([^\\_]*)\\_(?!.*`.*|.*<\\/code>.*)");
|
||||
static std::string replacement = "<em>$1</em>";
|
||||
|
||||
line = std::regex_replace(line, re, replacement);
|
||||
|
50
include/maddy/italicparser.h
Normal file
50
include/maddy/italicparser.h
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. For more information see the
|
||||
* LICENSE file.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <string>
|
||||
#include <regex>
|
||||
|
||||
#include "maddy/lineparser.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace maddy {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* ItalicParser
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class ItalicParser : public LineParser
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Parse
|
||||
*
|
||||
* From Markdown: `text *text*`
|
||||
*
|
||||
* To HTML: `text <i>text</i>`
|
||||
*
|
||||
* @method
|
||||
* @param {std::string&} line The line to interpret
|
||||
* @return {void}
|
||||
*/
|
||||
void
|
||||
Parse(std::string& line) override
|
||||
{
|
||||
std::regex re("(?!.*`.*|.*<code>.*)\\*(?!.*`.*|.*<\\/code>.*)([^\\*]*)\\*(?!.*`.*|.*<\\/code>.*)");
|
||||
static std::string replacement = "<i>$1</i>";
|
||||
line = std::regex_replace(line, re, replacement);
|
||||
}
|
||||
}; // class ItalicParser
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
} // namespace maddy
|
@ -25,6 +25,7 @@
|
||||
#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"
|
||||
@ -56,6 +57,7 @@ public:
|
||||
: 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>())
|
||||
@ -112,6 +114,7 @@ private:
|
||||
std::shared_ptr<EmphasizedParser> emphasizedParser;
|
||||
std::shared_ptr<ImageParser> imageParser;
|
||||
std::shared_ptr<InlineCodeParser> inlineCodeParser;
|
||||
std::shared_ptr<ItalicParser> italicParser;
|
||||
std::shared_ptr<LinkParser> linkParser;
|
||||
std::shared_ptr<StrikeThroughParser> strikeThroughParser;
|
||||
std::shared_ptr<StrongParser> strongParser;
|
||||
@ -131,6 +134,8 @@ private:
|
||||
this->strikeThroughParser->Parse(line);
|
||||
|
||||
this->inlineCodeParser->Parse(line);
|
||||
|
||||
this->italicParser->Parse(line);
|
||||
}
|
||||
|
||||
std::shared_ptr<BlockParser>
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
TEST(MADDY_EMPHASIZEDPARSER, ItReplacesMarkdownWithEmphasizedHTML)
|
||||
{
|
||||
std::string text = "some text *bla* text testing *it* out";
|
||||
std::string text = "some text _bla_ text testing _it_ out";
|
||||
std::string expected = "some text <em>bla</em> text testing <em>it</em> out";
|
||||
auto emphasizedParser = std::make_shared<maddy::EmphasizedParser>();
|
||||
|
||||
@ -23,7 +23,7 @@ TEST(MADDY_EMPHASIZEDPARSER, ItReplacesMarkdownWithEmphasizedHTML)
|
||||
|
||||
TEST(MADDY_EMPHASIZEDPARSER, ItDoesNotParseInsideInlineCode)
|
||||
{
|
||||
std::string text = "some text `*bla*` `/**text*/` testing *it* out";
|
||||
std::string text = "some text `*bla*` `/**text*/` testing _it_ out";
|
||||
std::string expected = "some text `*bla*` `/**text*/` testing <em>it</em> out";
|
||||
auto emphasizedParser = std::make_shared<maddy::EmphasizedParser>();
|
||||
|
||||
|
23
tests/maddy/test_maddy_italicparser.cpp
Normal file
23
tests/maddy/test_maddy_italicparser.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. For more information see the
|
||||
* LICENSE file.
|
||||
*/
|
||||
#include <memory>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
#include "maddy/italicparser.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
TEST(MADDY_ITALICPARSER, ItReplacesMarkdownWithItalicHTML)
|
||||
{
|
||||
|
||||
std::string text = "some text *bla* text testing *it* out";
|
||||
std::string expected = "some text <i>bla</i> text testing <i>it</i> out";
|
||||
auto italicParser = std::make_shared<maddy::ItalicParser>();
|
||||
|
||||
italicParser->Parse(text);
|
||||
|
||||
ASSERT_EQ(text, expected);
|
||||
}
|
@ -13,7 +13,7 @@ it's that simple.\n\
|
||||
\n\
|
||||
* an unordered list\n\
|
||||
* with some **hierarchy**\n\
|
||||
1. and an *ordered*\n\
|
||||
1. and an _ordered_\n\
|
||||
* list\n\
|
||||
* directly\n\
|
||||
* inside\n\
|
||||
|
Loading…
Reference in New Issue
Block a user