annotate 3rdparty/maddy/imageparser.h @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 /*
ferencd@0 2 * This project is licensed under the MIT license. For more information see the
ferencd@0 3 * LICENSE file.
ferencd@0 4 */
ferencd@0 5 #pragma once
ferencd@0 6
ferencd@0 7 // -----------------------------------------------------------------------------
ferencd@0 8
ferencd@0 9 #include <string>
ferencd@0 10 #include <regex>
ferencd@0 11
ferencd@0 12 #include "maddy/lineparser.h"
ferencd@0 13
ferencd@0 14 // -----------------------------------------------------------------------------
ferencd@0 15
ferencd@0 16 namespace maddy {
ferencd@0 17
ferencd@0 18 // -----------------------------------------------------------------------------
ferencd@0 19
ferencd@0 20 /**
ferencd@0 21 * ImageParser
ferencd@0 22 *
ferencd@0 23 * Has to be used before the `LinkParser`.
ferencd@0 24 *
ferencd@0 25 * @class
ferencd@0 26 */
ferencd@0 27 class ImageParser : public LineParser
ferencd@0 28 {
ferencd@0 29 public:
ferencd@0 30 /**
ferencd@0 31 * Parse
ferencd@0 32 *
ferencd@0 33 * From Markdown: `![text](http://example.com/a.png)`
ferencd@0 34 *
ferencd@0 35 * To HTML: `<img src="http://example.com/a.png" alt="text"/>`
ferencd@0 36 *
ferencd@0 37 * @method
ferencd@0 38 * @param {std::string&} line The line to interpret
ferencd@0 39 * @return {void}
ferencd@0 40 */
ferencd@0 41 void
ferencd@0 42 Parse(std::string& line) override
ferencd@0 43 {
ferencd@0 44 static std::regex re("\\!\\[([^\\]]*)\\]\\(([^\\]]*)\\)");
ferencd@0 45 static std::string replacement = "<img src=\"$2\" alt=\"$1\"/>";
ferencd@0 46
ferencd@0 47 line = std::regex_replace(line, re, replacement);
ferencd@0 48 }
ferencd@0 49 }; // class ImageParser
ferencd@0 50
ferencd@0 51 // -----------------------------------------------------------------------------
ferencd@0 52
ferencd@0 53 } // namespace maddy