From a9fa98b644634aae2a8fa322763d536928e3beaa Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 17 Aug 2025 00:08:26 -0400 Subject: [PATCH] Parser now works on the same slides but no per-slide metadata yet. --- main.cpp | 5 ++- md_parser.cpp | 67 ++++++++++++++++++++----------- md_parser.rl | 45 +++++++++++++++------ parser.cpp | 14 +++++-- parser.hpp | 7 ++++ sample/01-a-good-first-program.md | 1 - tests/parsing.cpp | 3 ++ 7 files changed, 101 insertions(+), 41 deletions(-) diff --git a/main.cpp b/main.cpp index 1e72e45..dc290c8 100644 --- a/main.cpp +++ b/main.cpp @@ -26,7 +26,10 @@ int main(int argc, char *argv[]) { presenter.setFramerateLimit(FRAME_LIMIT); presenter.setVerticalSyncEnabled(VSYNC); - auto data = parse_slides(argv[1]); + auto contents = load_file(argv[1]); + Parser rp; + rp.parse(contents); + auto data = rp.results(); SlidesUI slides(data); slides.init(); diff --git a/md_parser.cpp b/md_parser.cpp index 2fbde8d..6e3d353 100644 --- a/md_parser.cpp +++ b/md_parser.cpp @@ -3,12 +3,18 @@ #include #include +enum { + PLAIN=0, + TITLE=1, + ENUM=2 +}; -#line 45 "md_parser.rl" +#line 66 "md_parser.rl" -#line 7 "md_parser.cpp" + +#line 13 "md_parser.cpp" static const char _Parser_actions[] = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, @@ -97,7 +103,7 @@ static const int Parser_error = 0; static const int Parser_en_main = 1; -#line 48 "md_parser.rl" +#line 69 "md_parser.rl" bool Parser::parse(const std::string& input) { @@ -110,14 +116,14 @@ bool Parser::parse(const std::string& input) { std::string tk; -#line 105 "md_parser.cpp" +#line 111 "md_parser.cpp" { cs = Parser_start; } -#line 60 "md_parser.rl" +#line 81 "md_parser.rl" -#line 108 "md_parser.cpp" +#line 114 "md_parser.cpp" { int _klen; unsigned int _trans; @@ -192,44 +198,59 @@ _match: switch ( *_acts++ ) { case 0: -#line 12 "md_parser.rl" +#line 14 "md_parser.rl" { start = p; } break; case 1: -#line 13 "md_parser.rl" - { fmt::println("TITLE"); } +#line 15 "md_parser.rl" + { last = TITLE; } break; case 2: -#line 14 "md_parser.rl" - { fmt::println("ENUM"); } +#line 17 "md_parser.rl" + { last = ENUM; } break; case 3: -#line 15 "md_parser.rl" +#line 19 "md_parser.rl" { - tk = input.substr(start - begin, p - start); - std::cout << "CONTENT:" << std::quoted(tk) << '\n'; + if(last == TITLE) { + title = input.substr(start - begin, p - start); + } else if(last == ENUM) { + content += "* " + input.substr(start - begin, p - start); + content += "\n"; + } else { + content += input.substr(start - begin, p - start); + content += "\n"; + } + + last = PLAIN; } break; case 4: -#line 19 "md_parser.rl" +#line 32 "md_parser.rl" { tk = input.substr(start - begin, p - start); - std::cout << "META:" << std::quoted(tk) << '\n'; + deck->config = json::parse(tk); + std::cout << "META:" << deck->config << '\n'; } break; case 5: -#line 23 "md_parser.rl" +#line 38 "md_parser.rl" { fmt::println("----- START"); } break; case 6: -#line 25 "md_parser.rl" - { fmt::println(">> START SLIDE"); } +#line 40 "md_parser.rl" + { + title.clear(); + content.clear(); + } break; case 7: -#line 26 "md_parser.rl" - { fmt::println("<< END SLIDE"); } +#line 45 "md_parser.rl" + { + deck->slides.emplace_back(title, content, config); + } break; -#line 211 "md_parser.cpp" +#line 232 "md_parser.cpp" } } @@ -242,7 +263,7 @@ _again: _out: {} } -#line 61 "md_parser.rl" +#line 82 "md_parser.rl" bool good = pe - p == 0; diff --git a/md_parser.rl b/md_parser.rl index 820367e..882898d 100644 --- a/md_parser.rl +++ b/md_parser.rl @@ -1,29 +1,50 @@ #include #include +enum { + PLAIN=0, + TITLE=1, + ENUM=2 +}; + %%{ machine Parser; alphtype char; - action token { - tk = input.substr(start - begin, fpc - start); - std::cout << "TOKEN: " << std::quoted(tk) << '\n'; - } action mark { start = fpc; } - action title { fmt::println("TITLE"); } - action enum { fmt::println("ENUM"); } + action title { last = TITLE; } + + action enum { last = ENUM; } + action content { - tk = input.substr(start - begin, fpc - start); - std::cout << "CONTENT:" << std::quoted(tk) << '\n'; + if(last == TITLE) { + title = input.substr(start - begin, fpc - start); + } else if(last == ENUM) { + content += "* " + input.substr(start - begin, fpc - start); + content += "\n"; + } else { + content += input.substr(start - begin, fpc - start); + content += "\n"; + } + + last = PLAIN; } action meta { tk = input.substr(start - begin, fpc - start); - std::cout << "META:" << std::quoted(tk) << '\n'; + deck->config = json::parse(tk); + std::cout << "META:" << deck->config << '\n'; } + action start { fmt::println("----- START"); } - action plain { fmt::println("PLAIN"); } - action start_slide { fmt::println(">> START SLIDE"); } - action end_slide { fmt::println("<< END SLIDE"); } + + action start_slide { + title.clear(); + content.clear(); + } + + action end_slide { + deck->slides.emplace_back(title, content, config); + } eol = '\n'; start = '===' %start; diff --git a/parser.cpp b/parser.cpp index cc73a4e..f10e81b 100644 --- a/parser.cpp +++ b/parser.cpp @@ -5,19 +5,25 @@ #include #include "parser.hpp" #include "dbc.hpp" +#include +#include +#include + +using std::string, nlohmann::json, std::shared_ptr, std::make_shared; -#include "./md_parser.cpp" -std::string load_file(const std::string& md_file) { +string load_file(const string& md_file) { std::ifstream in_file{md_file, std::ios::binary}; dbc::check(bool(in_file), fmt::format("failed to load {}", md_file)); - return std::string(std::istreambuf_iterator(in_file), std::istreambuf_iterator()); + return string(std::istreambuf_iterator(in_file), std::istreambuf_iterator()); } +#include "./md_parser.cpp" + Parser::Parser() { - + deck = make_shared(); } void Parser::finalize() { diff --git a/parser.hpp b/parser.hpp index 30ad7e0..8c17de1 100644 --- a/parser.hpp +++ b/parser.hpp @@ -8,9 +8,16 @@ std::shared_ptr parse_slides(const std::string& md_file); std::string load_file(const std::string& md_file); struct Parser { + int last = 0; bool error = false; + std::shared_ptr deck; + + std::string title; + std::string content; + nlohmann::json config; Parser(); bool parse(const std::string& input); void finalize(); + std::shared_ptr results() { return deck; } }; diff --git a/sample/01-a-good-first-program.md b/sample/01-a-good-first-program.md index 95a4036..45807cf 100644 --- a/sample/01-a-good-first-program.md +++ b/sample/01-a-good-first-program.md @@ -8,7 +8,6 @@ * The key is this * That's what we want --- -{"bg_color": [255, 0, 0, 255]} # What You Should See Yes, but with more feeling. diff --git a/tests/parsing.cpp b/tests/parsing.cpp index 4169f92..a21d981 100644 --- a/tests/parsing.cpp +++ b/tests/parsing.cpp @@ -16,4 +16,7 @@ TEST_CASE("parse a simple example", "[parsing]") { rp.parse(contents); REQUIRE(!rp.error); + auto deck = rp.results(); + + REQUIRE(deck->slides.size() > 0); }