Skip to main content

mdsee_layout/
model.rs

1//! Layout model(design.md §16〜§19)。
2
3/// Layout後のドキュメント(§16)。
4#[derive(Debug, Clone, Default)]
5pub struct LayoutDocument {
6    pub blocks: Vec<LayoutBlock>,
7}
8
9/// Layout block(§17)。
10///
11/// Sprint 3で `Table` を追加。`Image` は担当Sprint(S4-9)で追加する。
12#[derive(Debug, Clone, PartialEq)]
13pub enum LayoutBlock {
14    Text(TextBlock),
15    Code(CodeLayout),
16    Table(TableLayout),
17    Rule(RuleLayout),
18}
19
20/// テキスト領域(§18)。
21#[derive(Debug, Clone, PartialEq)]
22pub struct TextBlock {
23    pub lines: Vec<LayoutLine>,
24}
25
26/// 1論理行(§18)。
27#[derive(Debug, Clone, PartialEq)]
28pub struct LayoutLine {
29    pub spans: Vec<LayoutSpan>,
30}
31
32/// 1装飾区切り(§18)。linkはSprint 2(S2-4)でOSC 8に使う。
33#[derive(Debug, Clone, PartialEq)]
34pub struct LayoutSpan {
35    pub content: String,
36    pub style: SemanticStyle,
37    pub link: Option<LinkTarget>,
38}
39
40/// Hyperlink target(§18)。Sprint 1では未使用。
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct LinkTarget {
43    pub url: String,
44}
45
46/// コード領域(§17, §28)。
47///
48/// 枠の描画はrender段階の責務(§5 borders)。`width` は枠の幅として
49/// 使うcontent幅。コード本文は折り返さない(S3-1)。
50#[derive(Debug, Clone, PartialEq)]
51pub struct CodeLayout {
52    pub language: Option<String>,
53    pub lines: Vec<String>,
54    /// 枠の幅(grapheme表示幅ではなく列数)。
55    pub width: usize,
56}
57
58/// 表領域(§17, §30〜§32)。
59///
60/// `TableLayoutEngine` がalignment調整・cell wrap・罫線まで完了した
61/// 表示済み行を保持する。renderはこの行をそのまま出力する。
62#[derive(Debug, Clone, PartialEq)]
63pub struct TableLayout {
64    pub lines: Vec<LayoutLine>,
65}
66
67/// 水平罫線領域(§17, §11 HorizontalRule)。
68#[derive(Debug, Clone, PartialEq)]
69pub struct RuleLayout {
70    pub width: usize,
71}
72
73/// 意味スタイル(§19)。色は持たず、Theme側で実際の色へ変換する。
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum SemanticStyle {
76    Body,
77    Muted,
78
79    Heading1,
80    Heading2,
81    Heading3,
82    Heading4,
83    Heading5,
84    Heading6,
85
86    Strong,
87    Emphasis,
88    Strike,
89
90    InlineCode,
91
92    Link,
93
94    Quote,
95
96    Code,
97
98    Border,
99
100    AlertNote,
101    AlertTip,
102    AlertImportant,
103    AlertWarning,
104    AlertCaution,
105}
106
107impl SemanticStyle {
108    /// 見出しレベルから対応するstyleを返す(§24)。
109    pub fn heading(level: u8) -> Self {
110        match level {
111            1 => Self::Heading1,
112            2 => Self::Heading2,
113            3 => Self::Heading3,
114            4 => Self::Heading4,
115            5 => Self::Heading5,
116            _ => Self::Heading6,
117        }
118    }
119}