#pragma once
// Spec: SPEC.md UX-MD-01. AST to view tree, mimicking MarkdownUI with the DevPlace theme
// (UI/Style/MarkdownTheme.swift). Values live in spec/MarkdownTheme.hpp.
#include <string>
#include <vector>
#include "ui/MarkdownParser.hpp"
#include "ui/Palette.hpp"
#include "ui/Views.hpp"
namespace dp::ui {
class MarkdownRenderer {
public:
explicit MarkdownRenderer(float baseFontSize);
ViewPtr render(const MarkdownDocument& doc) const;
private:
struct Ctx {
float size = 17;
Color color;
bool tight = false; // inside a tight list item: no paragraph bottom margin
int listDepth = 0;
};
struct InlineStyle {
float size = 17;
Weight weight = Weight::Regular;
bool italic = false;
bool strike = false;
Color color;
std::string link;
};
struct Built {
ViewPtr view;
float top = 0, bottom = 0;
};
struct Flow {
std::vector<StyledSpan> spans;
std::vector<ViewPtr> views;
};
static ViewPtr join(const std::vector<Built>& blocks);
ViewPtr stack(const std::vector<MdBlock>& blocks, const Ctx& ctx) const;
Built block(const MdBlock& b, const Ctx& ctx) const;
Built heading(const MdBlock& b, const Ctx& ctx) const;
Built paragraph(const MdBlock& b, const Ctx& ctx) const;
Built quote(const MdBlock& b, const Ctx& ctx) const;
Built codeBlock(const MdBlock& b, const Ctx& ctx) const;
Built list(const MdBlock& b, const Ctx& ctx) const;
Built table(const MdBlock& b, const Ctx& ctx) const;
Built rule(const Ctx& ctx) const;
ViewPtr marker(const MdBlock& list, const MdBlock& item, int index, const Ctx& ctx) const;
void collect(const std::vector<MdInline>& nodes, const InlineStyle& st, bool allowImages, Flow& flow) const;
static void flushSpans(Flow& flow, float lineSpacing);
ViewPtr flowView(const std::vector<MdInline>& nodes, const InlineStyle& st, bool allowImages, float lineSpacing) const;
static FontSpec fontOf(const InlineStyle& st, bool mono = false);
float base_;
};
} // namespace dp::ui