piki-gui 0.2.0

FLTK-based GUI for Piki personal wiki application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Markdown Abstract Syntax Tree
// Represents the parsed structure of a Markdown document

use std::fmt;

/// Unique identifier for AST nodes
pub type NodeId = usize;

/// Text styling attributes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TextStyle {
    pub bold: bool,
    pub italic: bool,
    pub code: bool,
    pub strikethrough: bool,
    pub underline: bool,
    pub highlight: bool,
}

/// Alignment options for text
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Alignment {
    Left,
    Center,
    Right,
}

/// Types of Markdown nodes
#[derive(Debug, Clone, PartialEq)]
pub enum NodeType {
    /// Root document node
    Document,

    /// Block-level elements
    Paragraph,
    Heading {
        level: u8,
    }, // 1-6
    CodeBlock {
        language: Option<String>,
        fence_info: String,
    },
    BlockQuote,
    List {
        ordered: bool,
        start: u64,
    },
    ListItem {
        checkbox: Option<bool>,
    },
    ThematicBreak, // Horizontal rule

    /// Inline elements
    Text {
        content: String,
        style: TextStyle,
    },
    SoftBreak,
    HardBreak,
    Link {
        destination: String,
        title: Option<String>,
    },
    Image {
        destination: String,
        title: Option<String>,
    },
    Code {
        content: String,
    },

    /// Extensions
    WikiLink {
        destination: String,
    }, // [[page]]
    Table,
    TableHead,
    TableRow,
    TableCell {
        alignment: Option<Alignment>,
    },
}

impl NodeType {
    /// Returns true if this node type is a block-level element
    pub fn is_block(&self) -> bool {
        matches!(
            self,
            NodeType::Document
                | NodeType::Paragraph
                | NodeType::Heading { .. }
                | NodeType::CodeBlock { .. }
                | NodeType::BlockQuote
                | NodeType::List { .. }
                | NodeType::ListItem { .. }
                | NodeType::ThematicBreak
                | NodeType::Table
                | NodeType::TableHead
                | NodeType::TableRow
        )
    }

    /// Returns true if this node type is an inline element
    pub fn is_inline(&self) -> bool {
        matches!(
            self,
            NodeType::Text { .. }
                | NodeType::SoftBreak
                | NodeType::HardBreak
                | NodeType::Link { .. }
                | NodeType::Image { .. }
                | NodeType::Code { .. }
                | NodeType::WikiLink { .. }
        )
    }

    /// Returns true if this node can have children
    pub fn can_have_children(&self) -> bool {
        !matches!(
            self,
            NodeType::Text { .. }
                | NodeType::SoftBreak
                | NodeType::HardBreak
                | NodeType::Code { .. }
                | NodeType::ThematicBreak
        )
    }
}

/// Computed style information for a node
/// This is calculated based on the node type and can be cached
#[derive(Debug, Clone)]
pub struct ComputedStyle {
    pub font_face: u8,        // Font index
    pub font_size: u8,        // Font size in points
    pub color: u32,           // RGBA color
    pub bgcolor: Option<u32>, // Optional background color
    pub underline: bool,
    pub line_height: f32, // Multiplier (e.g., 1.2)
}

impl Default for ComputedStyle {
    fn default() -> Self {
        ComputedStyle {
            font_face: 0,
            font_size: 14,
            color: 0x000000FF,
            bgcolor: None,
            underline: false,
            line_height: 1.2,
        }
    }
}

/// An AST node representing an element in the document tree
#[derive(Debug, Clone)]
pub struct ASTNode {
    /// Unique identifier for this node
    pub id: NodeId,

    /// The type and data of this node
    pub node_type: NodeType,

    /// Character position range in the source text
    /// These are byte offsets in the original markdown
    pub char_start: usize,
    pub char_end: usize,

    /// Child nodes
    pub children: Vec<ASTNode>,

    /// Parent node ID (None for root)
    pub parent_id: Option<NodeId>,

    /// Cached computed style for this node
    pub style: ComputedStyle,

    /// Metadata flags
    pub dirty: bool, // Needs re-layout
}

impl ASTNode {
    /// Create a new AST node
    pub fn new(id: NodeId, node_type: NodeType, char_start: usize, char_end: usize) -> Self {
        let style = Self::compute_style_for_type(&node_type);

        ASTNode {
            id,
            node_type,
            char_start,
            char_end,
            children: Vec::new(),
            parent_id: None,
            style,
            dirty: true,
        }
    }

    /// Compute default style based on node type
    fn compute_style_for_type(node_type: &NodeType) -> ComputedStyle {
        let mut style = ComputedStyle::default();

        match node_type {
            NodeType::Heading { level } => {
                style.font_face = 1; // Bold
                style.font_size = match level {
                    1 => 20,
                    2 => 18,
                    3 => 16,
                    _ => 14,
                };
            }
            NodeType::Code { .. } | NodeType::CodeBlock { .. } => {
                style.font_face = 4; // Courier/monospace
                style.color = 0x006400FF; // Dark green
                style.bgcolor = Some(0xF5F5F5FF); // Light gray
            }
            NodeType::Link { .. } | NodeType::WikiLink { .. } => {
                style.color = 0x0000FFFF; // Blue
                style.underline = true;
            }
            NodeType::BlockQuote => {
                style.font_face = 3; // Italic
                style.color = 0x640000FF; // Dark red
            }
            NodeType::Text {
                style: text_style, ..
            } => {
                if text_style.bold && text_style.italic {
                    style.font_face = 3; // Bold+Italic
                } else if text_style.bold {
                    style.font_face = 1; // Bold
                } else if text_style.italic {
                    style.font_face = 2; // Italic
                }

                if text_style.code {
                    style.font_face = 4; // Monospace
                    style.color = 0x006400FF; // Dark green
                }
            }
            _ => {}
        }

        style
    }

    /// Add a child node and set its parent
    pub fn add_child(&mut self, mut child: ASTNode) {
        child.parent_id = Some(self.id);
        self.children.push(child);
    }

    /// Get the text content of this node (for Text nodes)
    pub fn text_content(&self) -> Option<&str> {
        match &self.node_type {
            NodeType::Text { content, .. } => Some(content),
            NodeType::Code { content } => Some(content),
            _ => None,
        }
    }

    /// Get all text content recursively (flattened)
    pub fn flatten_text(&self) -> String {
        let mut result = String::new();
        self.flatten_text_recursive(&mut result);
        result
    }

    fn flatten_text_recursive(&self, buffer: &mut String) {
        match &self.node_type {
            NodeType::Text { content, .. } => {
                buffer.push_str(content);
            }
            NodeType::Code { content } => {
                buffer.push_str(content);
            }
            NodeType::SoftBreak => {
                buffer.push(' ');
            }
            NodeType::HardBreak => {
                buffer.push('\n');
            }
            _ => {
                for child in &self.children {
                    child.flatten_text_recursive(buffer);
                }
            }
        }
    }

    /// Check if a position falls within this node's range
    pub fn contains_position(&self, pos: usize) -> bool {
        pos >= self.char_start && pos < self.char_end
    }

    /// Update character positions by offset (used after edits)
    pub fn adjust_positions(&mut self, from_pos: usize, delta: isize) {
        if self.char_start >= from_pos {
            self.char_start = (self.char_start as isize + delta).max(0) as usize;
        }
        if self.char_end >= from_pos {
            self.char_end = (self.char_end as isize + delta).max(0) as usize;
        }

        for child in &mut self.children {
            child.adjust_positions(from_pos, delta);
        }
    }

    /// Mark this node and all ancestors as dirty
    pub fn mark_dirty(&mut self) {
        self.dirty = true;
    }
}

impl fmt::Display for ASTNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.fmt_recursive(f, 0)
    }
}

impl ASTNode {
    fn fmt_recursive(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result {
        let indent_str = "  ".repeat(indent);

        write!(f, "{}", indent_str)?;

        match &self.node_type {
            NodeType::Document => writeln!(f, "Document [{}-{}]", self.char_start, self.char_end)?,
            NodeType::Paragraph => {
                writeln!(f, "Paragraph [{}-{}]", self.char_start, self.char_end)?
            }
            NodeType::Heading { level } => writeln!(
                f,
                "Heading(h{}) [{}-{}]",
                level, self.char_start, self.char_end
            )?,
            NodeType::Text { content, style } => writeln!(
                f,
                "Text({:?}): {:?} [{}-{}]",
                style, content, self.char_start, self.char_end
            )?,
            NodeType::Link { destination, .. } => writeln!(
                f,
                "Link -> {:?} [{}-{}]",
                destination, self.char_start, self.char_end
            )?,
            NodeType::CodeBlock { language, .. } => writeln!(
                f,
                "CodeBlock({:?}) [{}-{}]",
                language, self.char_start, self.char_end
            )?,
            NodeType::List { ordered, .. } => writeln!(
                f,
                "List({}) [{}-{}]",
                if *ordered { "ordered" } else { "unordered" },
                self.char_start,
                self.char_end
            )?,
            _ => writeln!(
                f,
                "{:?} [{}-{}]",
                self.node_type, self.char_start, self.char_end
            )?,
        }

        for child in &self.children {
            child.fmt_recursive(f, indent + 1)?;
        }

        Ok(())
    }
}

/// The complete document AST with position index
pub struct Document {
    /// Root node of the AST
    pub root: ASTNode,

    /// Source text that was parsed
    pub source: String,

    /// Next node ID to assign
    next_id: NodeId,
}

impl Document {
    /// Create a new empty document
    pub fn new() -> Self {
        let root = ASTNode::new(0, NodeType::Document, 0, 0);

        Document {
            root,
            source: String::new(),
            next_id: 1,
        }
    }

    /// Get the next available node ID
    pub fn next_id(&mut self) -> NodeId {
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    /// Find a node by ID (depth-first search)
    pub fn find_node(&self, id: NodeId) -> Option<&ASTNode> {
        Self::find_node_recursive(&self.root, id)
    }

    fn find_node_recursive(node: &ASTNode, id: NodeId) -> Option<&ASTNode> {
        if node.id == id {
            return Some(node);
        }

        for child in &node.children {
            if let Some(found) = Self::find_node_recursive(child, id) {
                return Some(found);
            }
        }

        None
    }

    /// Find node at a specific character position
    pub fn find_node_at_position(&self, pos: usize) -> Option<&ASTNode> {
        Self::find_node_at_position_recursive(&self.root, pos)
    }

    fn find_node_at_position_recursive(node: &ASTNode, pos: usize) -> Option<&ASTNode> {
        if !node.contains_position(pos) {
            return None;
        }

        // Check children first (most specific match)
        for child in &node.children {
            if let Some(found) = Self::find_node_at_position_recursive(child, pos) {
                return Some(found);
            }
        }

        // If no child contains it, this node is the most specific
        Some(node)
    }

    /// Get the plain text representation
    pub fn to_text(&self) -> String {
        self.root.flatten_text()
    }

    /// Update source text and mark affected regions as dirty
    pub fn update_source(&mut self, new_source: String, changed_start: usize, changed_end: usize) {
        let old_len = self.source.len();
        self.source = new_source;
        let new_len = self.source.len();

        // Calculate position delta
        let delta = new_len as isize - old_len as isize;

        // Adjust positions of nodes after the change
        self.root.adjust_positions(changed_end, delta);

        // Mark affected nodes as dirty
        self.mark_region_dirty(changed_start, changed_end);
    }

    fn mark_region_dirty(&mut self, start: usize, end: usize) {
        Self::mark_node_dirty_recursive(&mut self.root, start, end);
    }

    fn mark_node_dirty_recursive(node: &mut ASTNode, start: usize, end: usize) {
        // Check if this node overlaps with the dirty region
        if node.char_end >= start && node.char_start <= end {
            node.mark_dirty();

            for child in &mut node.children {
                Self::mark_node_dirty_recursive(child, start, end);
            }
        }
    }
}

impl Default for Document {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for Document {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Document (source: {} bytes)", self.source.len())?;
        write!(f, "{}", self.root)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_node_creation() {
        let node = ASTNode::new(0, NodeType::Paragraph, 0, 10);
        assert_eq!(node.id, 0);
        assert_eq!(node.char_start, 0);
        assert_eq!(node.char_end, 10);
        assert!(node.dirty);
    }

    #[test]
    fn test_add_child() {
        let mut parent = ASTNode::new(0, NodeType::Paragraph, 0, 10);
        let child = ASTNode::new(
            1,
            NodeType::Text {
                content: "hello".to_string(),
                style: TextStyle::default(),
            },
            0,
            5,
        );

        parent.add_child(child);

        assert_eq!(parent.children.len(), 1);
        assert_eq!(parent.children[0].parent_id, Some(0));
    }

    #[test]
    fn test_flatten_text() {
        let mut para = ASTNode::new(0, NodeType::Paragraph, 0, 15);
        para.add_child(ASTNode::new(
            1,
            NodeType::Text {
                content: "hello".to_string(),
                style: TextStyle::default(),
            },
            0,
            5,
        ));
        para.add_child(ASTNode::new(
            2,
            NodeType::Text {
                content: " world".to_string(),
                style: TextStyle::default(),
            },
            5,
            11,
        ));

        assert_eq!(para.flatten_text(), "hello world");
    }

    #[test]
    fn test_contains_position() {
        let node = ASTNode::new(0, NodeType::Paragraph, 10, 20);
        assert!(!node.contains_position(5));
        assert!(node.contains_position(10));
        assert!(node.contains_position(15));
        assert!(!node.contains_position(20));
    }

    #[test]
    fn test_adjust_positions() {
        let mut node = ASTNode::new(0, NodeType::Paragraph, 10, 20);
        let child = ASTNode::new(
            1,
            NodeType::Text {
                content: "test".to_string(),
                style: TextStyle::default(),
            },
            10,
            14,
        );
        node.add_child(child);

        node.adjust_positions(5, 3); // Insert 3 chars at position 5

        assert_eq!(node.char_start, 13);
        assert_eq!(node.char_end, 23);
        assert_eq!(node.children[0].char_start, 13);
        assert_eq!(node.children[0].char_end, 17);
    }

    #[test]
    fn test_document_find_node_at_position() {
        let mut doc = Document::new();
        doc.root.char_end = 10; // Set root to cover the range

        let mut para = ASTNode::new(doc.next_id(), NodeType::Paragraph, 0, 10);
        para.add_child(ASTNode::new(
            doc.next_id(),
            NodeType::Text {
                content: "hello".to_string(),
                style: TextStyle::default(),
            },
            0,
            5,
        ));

        doc.root.add_child(para);

        let node = doc.find_node_at_position(3);
        assert!(node.is_some());
        assert!(matches!(node.unwrap().node_type, NodeType::Text { .. }));
    }
}