diff options
| author | vvvv <[email protected]> | 2025-10-06 11:26:09 +0300 |
|---|---|---|
| committer | vvvv <[email protected]> | 2025-10-06 11:53:26 +0300 |
| commit | 60f45e69a4d7dbc6131208e16c45faf35aa5a985 (patch) | |
| tree | 4daa45b52c295a178c7620e4c93921465fcf7950 /yql/essentials/utils/docs/markdown.cpp | |
| parent | 1bded1a65a7e6e9171418f3e1c691d390125b64e (diff) | |
YQL-20086 utils
init
commit_hash:54feccd520ebd0ab23612bc0cb830914dff9d0e8
Diffstat (limited to 'yql/essentials/utils/docs/markdown.cpp')
| -rw-r--r-- | yql/essentials/utils/docs/markdown.cpp | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/yql/essentials/utils/docs/markdown.cpp b/yql/essentials/utils/docs/markdown.cpp index fd13820b8c4..fb4209717e4 100644 --- a/yql/essentials/utils/docs/markdown.cpp +++ b/yql/essentials/utils/docs/markdown.cpp @@ -9,127 +9,127 @@ namespace NYql::NDocs { - class TMarkdownParser { - private: - static constexpr TStringBuf HeaderRegex = R"re(([^#]+)(\s+{#([a-z0-9\-_]+)})?)re"; - - public: - explicit TMarkdownParser(size_t headerDepth) - : HeaderDepth_(headerDepth) - , SectionHeaderRegex_(" *" + TString(HeaderDepth_, '#') + " " + HeaderRegex) - , IsSkipping_(true) - { - } +class TMarkdownParser { +private: + static constexpr TStringBuf HeaderRegex = R"re(([^#]+)(\s+{#([a-z0-9\-_]+)})?)re"; + +public: + explicit TMarkdownParser(size_t headerDepth) + : HeaderDepth_(headerDepth) + , SectionHeaderRegex_(" *" + TString(HeaderDepth_, '#') + " " + HeaderRegex) + , IsSkipping_(true) + { + } - void Parse(IInputStream& markdown, TMarkdownCallback&& onSection) { - for (TString line; markdown.ReadLine(line) != 0;) { - size_t depth = HeaderDepth(line); - if (IsSkipping_) { - if (HeaderDepth_ == depth) { - ResetSection(std::move(line)); - IsSkipping_ = false; - } else { - // Skip - } + void Parse(IInputStream& markdown, TMarkdownCallback&& onSection) { + for (TString line; markdown.ReadLine(line) != 0;) { + size_t depth = HeaderDepth(line); + if (IsSkipping_) { + if (HeaderDepth_ == depth) { + ResetSection(std::move(line)); + IsSkipping_ = false; } else { - if (HeaderDepth_ == depth) { - onSection(std::move(Section_)); - ResetSection(std::move(line)); - } else if (depth == 0 || HeaderDepth_ < depth) { - line.append('\n'); - Section_.Body.append(std::move(line)); - } else { - onSection(std::move(Section_)); - IsSkipping_ = true; - } + // Skip + } + } else { + if (HeaderDepth_ == depth) { + onSection(std::move(Section_)); + ResetSection(std::move(line)); + } else if (depth == 0 || HeaderDepth_ < depth) { + line.append('\n'); + Section_.Body.append(std::move(line)); + } else { + onSection(std::move(Section_)); + IsSkipping_ = true; } - } - - if (!IsSkipping_) { - onSection(std::move(Section_)); } } - private: - void ResetSection(TString&& line) { - Section_ = TMarkdownSection(); + if (!IsSkipping_) { + onSection(std::move(Section_)); + } + } - TString content; - std::optional<TString> dummy; - std::optional<TString> anchor; - if (!RE2::FullMatch(line, SectionHeaderRegex_, &content, &dummy, &anchor)) { - Section_.Header.Content = std::move(line); - return; - } +private: + void ResetSection(TString&& line) { + Section_ = TMarkdownSection(); - Section_.Header.Content = std::move(content); - if (anchor) { - Section_.Header.Anchor = std::move(*anchor); - } + TString content; + std::optional<TString> dummy; + std::optional<TString> anchor; + if (!RE2::FullMatch(line, SectionHeaderRegex_, &content, &dummy, &anchor)) { + Section_.Header.Content = std::move(line); + return; } - size_t HeaderDepth(TStringBuf line) const { - while (line.StartsWith(' ') || line.StartsWith('\t')) { - line.Skip(1); - } + Section_.Header.Content = std::move(content); + if (anchor) { + Section_.Header.Anchor = std::move(*anchor); + } + } - if (!line.StartsWith('#')) { - return 0; - } + size_t HeaderDepth(TStringBuf line) const { + while (line.StartsWith(' ') || line.StartsWith('\t')) { + line.Skip(1); + } - size_t begin = line.find('#'); - size_t end = line.find_first_not_of('#', begin); - return end != TStringBuf::npos ? (end - begin) : 0; + if (!line.StartsWith('#')) { + return 0; } - size_t HeaderDepth_; - RE2 SectionHeaderRegex_; - bool IsSkipping_; - TMarkdownSection Section_; - }; + size_t begin = line.find('#'); + size_t end = line.find_first_not_of('#', begin); + return end != TStringBuf::npos ? (end - begin) : 0; + } - TMaybe<TString> Anchor(const TMarkdownHeader& header) { - static RE2 Regex(R"re([0-9a-z\-_]+)re"); + size_t HeaderDepth_; + RE2 SectionHeaderRegex_; + bool IsSkipping_; + TMarkdownSection Section_; +}; - if (header.Anchor) { - return header.Anchor; - } +TMaybe<TString> Anchor(const TMarkdownHeader& header) { + static RE2 Regex(R"re([0-9a-z\-_]+)re"); - TString content = ToLowerUTF8(header.Content); - SubstGlobal(content, ' ', '-'); + if (header.Anchor) { + return header.Anchor; + } - if (RE2::FullMatch(content, Regex)) { - return content; - } + TString content = ToLowerUTF8(header.Content); + SubstGlobal(content, ' ', '-'); - return Nothing(); + if (RE2::FullMatch(content, Regex)) { + return content; } - TMarkdownPage ParseMarkdownPage(TString markdown) { - TMarkdownPage page; + return Nothing(); +} - const auto onSection = [&](TMarkdownSection&& section) { - if (TMaybe<TString> anchor = Anchor(section.Header)) { - section.Header.Anchor = anchor; - page.SectionsByAnchor[*anchor] = std::move(section); - } - }; +TMarkdownPage ParseMarkdownPage(TString markdown) { + TMarkdownPage page; - { - TMarkdownParser parser(/*headerDepth=*/2); - TStringStream stream(markdown); - parser.Parse(stream, onSection); - } - - { - TMarkdownParser parser(/*headerDepth=*/3); - TStringStream stream(markdown); - parser.Parse(stream, onSection); + const auto onSection = [&](TMarkdownSection&& section) { + if (TMaybe<TString> anchor = Anchor(section.Header)) { + section.Header.Anchor = anchor; + page.SectionsByAnchor[*anchor] = std::move(section); } + }; - page.Text = std::move(markdown); + { + TMarkdownParser parser(/*headerDepth=*/2); + TStringStream stream(markdown); + parser.Parse(stream, onSection); + } - return page; + { + TMarkdownParser parser(/*headerDepth=*/3); + TStringStream stream(markdown); + parser.Parse(stream, onSection); } + page.Text = std::move(markdown); + + return page; +} + } // namespace NYql::NDocs |
