diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a1204c5b31..8ea0c06ab5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -312,6 +312,7 @@ set( COMMON_SRCS lib_tree_model.cpp lib_tree_model_adapter.cpp lockfile.cpp + ../markdown2html/markdown2html.cpp marker_base.cpp md5_hash.cpp msgpanel.cpp diff --git a/include/dialog_helpers.h b/include/dialog_helpers.h index 6a194f05a1..36ac7f9519 100644 --- a/include/dialog_helpers.h +++ b/include/dialog_helpers.h @@ -35,6 +35,7 @@ #include // EDA_UNITS_T #include <../common/dialogs/dialog_list_selector_base.h> +void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput ); class EDA_DRAW_FRAME; diff --git a/markdown2html/definitions.md b/markdown2html/definitions.md new file mode 100644 index 0000000000..0b97ae1317 --- /dev/null +++ b/markdown2html/definitions.md @@ -0,0 +1,327 @@ +# Markdown Definitions + +This specification defines which markdown syntax can be parsed by maddy. +There is no HTML allowed in the markdown syntax - or said otherwise - it might +destroy the output, if there was HTML in your markdown. + +The Parser expects you to use spaces and not tabs for indentation in the +markdown. + +## Headlines + +``` +# h1 heading +## h2 heading +### h3 heading +#### h4 heading +##### h5 heading +###### h6 heading +``` +results in: +```html +

h1 heading

+

h2 heading

+

h3 heading

+

h4 heading

+
h5 heading
+
h6 heading
+``` + +## Links + +``` +[Text of the link](http://example.com) +``` +results in +```html +Text of the link +``` + +## Lists + +### unordered +``` + +* unordered +* list +* items + +``` +results in +```html + +``` + +``` + +* unordered + * list + * items + * in + * an + * hierarchy + +``` +results in +```html + +``` + +### ordered + +``` + +1. ordered +* list +* items + +``` + +results in + +```html + +
    +
  1. ordered
  2. +
  3. list
  4. +
  5. items
  6. +
+ +``` + +``` + +1. ordered +* list + 1. items + * in + 1. an + * hierarchy + +``` + +results in + +```html +
    +
  1. ordered
  2. +
  3. list +
      +
    1. items
    2. +
    3. in +
        +
      1. an
      2. +
      +
    4. +
    5. hierarchy
    6. +
    +
  4. +
+``` + +### combination + +``` + +* combination +* of + 1. unordered and + * ordered +* list + +``` +results in +```html + +``` + +### checklist + +``` + +- [ ] some item + - [ ] another item +- [x] some checked item + +``` +results in +```html + +``` +might not work in combination with other lists + +## Code Blocks + + ``` + some code + ``` + +results in +```html +

+some code
+
+``` + +## Inline code + + some text `some inline code` some other text + +results in +```html +some text some inline code some other text +``` + +## quotes + +``` +> Some quote +``` +results in +```html + +

Some quote

+ +``` + +## bold + +``` +**bold text** +__bold text__ +``` +results in +```html +bold text +bold text +``` + +## italic + +``` +*italic text* +``` +results in +```html +italic text +``` + +## emphasized + +``` +_emphasized text_ +``` +results in +```html +emphasized text +``` + +## strikethrough + +``` +~~striked through text~~ +``` +results in +```html +striked through text +``` + +## horizontal line + +``` +--- +``` +results in +```html +
+``` + +## Images + +``` +![Image alt text](http://example.com/example.png) +``` +results in +```html +Image alt text +``` + +## Tables + +``` + +|table> +Left header | middle header | last header +- | - | - +cell 1 | cell 2 | cell 3 +cell 4 | cell 5 | cell 6 +- | - | - +foot a | foot b | foot c +| + + + + + + + + + + + + + + + + + + + + + + + + + + +
Left headermiddle headerlast header
cell 1cell 2cell 3
cell 4cell 5cell 6
foot afoot bfoot c
+``` +table header and footer are optional diff --git a/markdown2html/markdown2html.cpp b/markdown2html/markdown2html.cpp new file mode 100644 index 0000000000..221df6a9be --- /dev/null +++ b/markdown2html/markdown2html.cpp @@ -0,0 +1,38 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include +#include + +#include +#include + +#include "md2html/parser.h" + + +void ConvertMarkdown2Html( const wxString& aMarkdownInput, wxString& aHtmlOutput ) +{ + std::stringstream markdownInput( TO_UTF8( aMarkdownInput ) ); + + std::shared_ptr parser = std::make_shared(); + std::string htmlOutput = parser->Parse(markdownInput); + + aHtmlOutput = FROM_UTF8( htmlOutput.c_str() ); +} diff --git a/markdown2html/md2html/AUTHORS.txt b/markdown2html/md2html/AUTHORS.txt new file mode 100644 index 0000000000..69966f5494 --- /dev/null +++ b/markdown2html/md2html/AUTHORS.txt @@ -0,0 +1,11 @@ +The following authors have all licensed their contributions to maddy under the +licensing terms detailed in LICENSE. + +(Authors keep copyright of their contributions, of course; they just grant +a license to everyone to use it as detailed in LICENSE.) + +M. Petra Baranski (info@progsource.de) +Patrick José Pereira (patrickelectric@gmail.com) + +Github site: +https://github.com/progsource/maddy diff --git a/markdown2html/md2html/LICENSE.txt b/markdown2html/md2html/LICENSE.txt new file mode 100644 index 0000000000..2333dfd242 --- /dev/null +++ b/markdown2html/md2html/LICENSE.txt @@ -0,0 +1,18 @@ +Copyright 2017, 2018, 2019 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/markdown2html/md2html/blockparser.h b/markdown2html/md2html/blockparser.h new file mode 100644 index 0000000000..af85626852 --- /dev/null +++ b/markdown2html/md2html/blockparser.h @@ -0,0 +1,195 @@ +/* + * 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} aParseLineCallback + * @param {std::function(const std::string& line)>} aGetBlockParserForLineCallback + */ + BlockParser( std::function aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : result( "", std::ios_base::ate | std::ios_base::in | std::ios_base::out ), + childParser( nullptr ), + parseLineCallback( aParseLineCallback ), + getBlockParserForLineCallback( aGetBlockParserForLineCallback ) + {} + + /** + * 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/markdown2html/md2html/checklistparser.h b/markdown2html/md2html/checklistparser.h new file mode 100644 index 0000000000..ca71516cac --- /dev/null +++ b/markdown2html/md2html/checklistparser.h @@ -0,0 +1,134 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { +// ----------------------------------------------------------------------------- + +/** + * ChecklistParser + * + * @class + */ +class ChecklistParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} aParseLineCallback + * @param {std::function(const std::string& line)>} aGetBlockParserForLineCallback + */ + ChecklistParser( std::function aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : BlockParser( aParseLineCallback, aGetBlockParserForLineCallback ), + 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\\. |\\* ).*" ); + + return std::regex_match( line, re ); + } +}; // class OrderedListParser + +// ----------------------------------------------------------------------------- +} // namespace maddy diff --git a/markdown2html/md2html/paragraphparser.h b/markdown2html/md2html/paragraphparser.h new file mode 100644 index 0000000000..bfa5934d4e --- /dev/null +++ b/markdown2html/md2html/paragraphparser.h @@ -0,0 +1,106 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "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 aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : BlockParser( aParseLineCallback, aGetBlockParserForLineCallback ), + isStarted( false ), + isFinished( false ) + {} + + /** + * IsStartingLine + * + * If the line is not empty, it will be a paragraph. So 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/markdown2html/md2html/parser.h b/markdown2html/md2html/parser.h new file mode 100644 index 0000000000..74baa8a83d --- /dev/null +++ b/markdown2html/md2html/parser.h @@ -0,0 +1,266 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +// BlockParser +#include "checklistparser.h" +#include "codeblockparser.h" +#include "headlineparser.h" +#include "horizontallineparser.h" +#include "orderedlistparser.h" +#include "paragraphparser.h" +#include "quoteparser.h" +#include "tableparser.h" +#include "unorderedlistparser.h" + +// LineParser +#include "emphasizedparser.h" +#include "imageparser.h" +#include "inlinecodeparser.h" +#include "italicparser.h" +#include "linkparser.h" +#include "strikethroughparser.h" +#include "strongparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { +// ----------------------------------------------------------------------------- + +/** + * Parser + * + * Transforms Markdown to HTML + * + * @class + */ +class Parser +{ +public: + /** + * ctor + * + * Initializes all `LineParser` + * + * @method + */ + Parser() + : 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::stringstream&} markdown + * @return {std::string} HTML + */ + std::string Parse( std::stringstream& 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 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 ); + this->emphasizedParser->Parse( line ); + + this->strikeThroughParser->Parse( line ); + + this->inlineCodeParser->Parse( line ); + + this->italicParser->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& aLine ) { this->runLineParser( aLine ); }, + [this]( const std::string& aLine ) + { return this->getBlockParserForLine( aLine ); } + ); + } + else if( maddy::TableParser::IsStartingLine( line ) ) + { + parser = std::make_shared( + [this]( std::string& aLine ) { this->runLineParser( aLine ); }, + 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( maddy::ParagraphParser::IsStartingLine( line ) ) + { + parser = std::make_shared( + [this]( std::string& aLine ) { this->runLineParser( aLine ); }, + 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/markdown2html/md2html/quoteparser.h b/markdown2html/md2html/quoteparser.h new file mode 100644 index 0000000000..4cb0017049 --- /dev/null +++ b/markdown2html/md2html/quoteparser.h @@ -0,0 +1,159 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "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 aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : BlockParser( aParseLineCallback, aGetBlockParserForLineCallback ), + 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/markdown2html/md2html/strikethroughparser.h b/markdown2html/md2html/strikethroughparser.h new file mode 100644 index 0000000000..2c9b1eade6 --- /dev/null +++ b/markdown2html/md2html/strikethroughparser.h @@ -0,0 +1,49 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "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/markdown2html/md2html/strongparser.h b/markdown2html/md2html/strongparser.h new file mode 100644 index 0000000000..3b4e65da0e --- /dev/null +++ b/markdown2html/md2html/strongparser.h @@ -0,0 +1,61 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "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/markdown2html/md2html/tableparser.h b/markdown2html/md2html/tableparser.h new file mode 100644 index 0000000000..7e3ebb51ae --- /dev/null +++ b/markdown2html/md2html/tableparser.h @@ -0,0 +1,239 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { +// ----------------------------------------------------------------------------- + +/** + * TableParser + * + * For more information, see the docs folder. + * + * @class + */ +class TableParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} aParseLineCallback + * @param {std::function(const std::string& line)>} aGetBlockParserForLineCallback + */ + TableParser( std::function aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : BlockParser( aParseLineCallback, aGetBlockParserForLineCallback ), + 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/markdown2html/md2html/unorderedlistparser.h b/markdown2html/md2html/unorderedlistparser.h new file mode 100644 index 0000000000..22f1c42035 --- /dev/null +++ b/markdown2html/md2html/unorderedlistparser.h @@ -0,0 +1,127 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { +// ----------------------------------------------------------------------------- + +/** + * UnorderedListParser + * + * @class + */ +class UnorderedListParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} aParseLineCallback + * @param {std::function(const std::string& line)>} aGetBlockParserForLineCallback + */ + UnorderedListParser( std::function aParseLineCallback, + std::function(const std::string& line)> aGetBlockParserForLineCallback + ) + : BlockParser( aParseLineCallback, aGetBlockParserForLineCallback ), + 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/pcb_calculator/CMakeLists.txt b/pcb_calculator/CMakeLists.txt index aad4cf5a12..3d4a721729 100644 --- a/pcb_calculator/CMakeLists.txt +++ b/pcb_calculator/CMakeLists.txt @@ -171,8 +171,22 @@ function( html_doc2h inputFile ) COMMENT "creating ${inputFile}.h from ${inputFile}.html" ) endfunction() +# +# Conversion of .md doc source files to .h files included in cpp sources +# +# Function md_doc2h : converts a single *.md text file to a *.h header +function( md_doc2h inputFile ) + add_custom_command( + OUTPUT ${inputFile}.h - html_doc2h( ${CMAKE_CURRENT_SOURCE_DIR}/attenuators/bridget_tee_formula ) + COMMAND ${CMAKE_COMMAND} -DinputFile=${inputFile}.md -DoutputFile=${inputFile}.h + -P ${CMAKE_MODULE_PATH}/markdown2C.cmake + DEPENDS ${inputFile}.md ${CMAKE_MODULE_PATH}/markdown2C.cmake + COMMENT "creating ${inputFile}.h from ${inputFile}.md" + ) +endfunction() + + md_doc2h( ${CMAKE_CURRENT_SOURCE_DIR}/attenuators/bridget_tee_formula ) html_doc2h( ${CMAKE_CURRENT_SOURCE_DIR}/attenuators/splitter_formula ) html_doc2h( ${CMAKE_CURRENT_SOURCE_DIR}/attenuators/pi_formula ) html_doc2h( ${CMAKE_CURRENT_SOURCE_DIR}/attenuators/tee_formula ) diff --git a/pcb_calculator/attenuators.cpp b/pcb_calculator/attenuators.cpp index 1fbb38766a..74f6930956 100644 --- a/pcb_calculator/attenuators.cpp +++ b/pcb_calculator/attenuators.cpp @@ -28,7 +28,7 @@ #include #include -#include +#include extern double DoubleFromString( const wxString& TextValue ); @@ -97,7 +97,16 @@ void PCB_CALCULATOR_FRAME::TransfAttenuatorDataToPanel() m_ZoutValueCtrl->SetValue( msg ); if( m_currAttenuator->m_FormulaName ) - m_panelAttFormula->SetPage( *m_currAttenuator->m_FormulaName ); + { + if( m_currAttenuator->m_FormulaName->StartsWith( "SetPage( *m_currAttenuator->m_FormulaName ); + else + { + wxString html_txt; + ConvertMarkdown2Html( wxGetTranslation( *m_currAttenuator->m_FormulaName ), html_txt ); + m_panelAttFormula->SetPage( html_txt ); + } + } else m_panelAttFormula->SetPage( wxEmptyString ); } diff --git a/pcb_calculator/attenuators/attenuator_classes.cpp b/pcb_calculator/attenuators/attenuator_classes.cpp index d7c15fd248..46c07bed15 100644 --- a/pcb_calculator/attenuators/attenuator_classes.cpp +++ b/pcb_calculator/attenuators/attenuator_classes.cpp @@ -8,6 +8,7 @@ #include #include +#include // Bitmaps: #include @@ -25,9 +26,9 @@ wxString tee_formula( #include ); -wxString bridget_tee_formula( +wxString bridget_tee_formula = #include -); +; wxString splitter_formula( #include diff --git a/pcb_calculator/attenuators/attenuator_classes.h b/pcb_calculator/attenuators/attenuator_classes.h index 34a8517b6b..c3672155f7 100644 --- a/pcb_calculator/attenuators/attenuator_classes.h +++ b/pcb_calculator/attenuators/attenuator_classes.h @@ -40,7 +40,7 @@ public: double m_R2; // value of R2 double m_R3; // value of R3 (if any) wxBitmap* m_SchBitMap; // The schema of this attenuator - wxString* m_FormulaName; // The HTML text name of the formula used to calculate this attenuator + wxString* m_FormulaName; // The HTML/markdown text name of the formula protected: double Lmin, L, A; // internal variable for temporary use diff --git a/pcb_calculator/attenuators/bridget_tee_formula.md b/pcb_calculator/attenuators/bridget_tee_formula.md new file mode 100644 index 0000000000..a7f3fd2d47 --- /dev/null +++ b/pcb_calculator/attenuators/bridget_tee_formula.md @@ -0,0 +1,8 @@ +__Zin__ desired input impedance in Ω
    +__Zout__ desired output impedance __Zin = Zout__ + +### Bridged tee attenuator: +* __a__ attenuation in dB +* __L = 10a/20__ (the loss) +* ___R1 = Zin * (L - 1)___ +* ___R2 = Zin / (L - 1)___