From eaadfca9b983d951c9c5e5e453bb47ef57037c4c Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Fri, 2 Aug 2024 10:32:00 +0200 Subject: [PATCH] Refactor lists --- src/elements/link.rs | 2 +- src/elements/list.rs | 728 +++++++++++++++++++++++++++---------------- src/elements/raw.rs | 25 +- 3 files changed, 467 insertions(+), 288 deletions(-) diff --git a/src/elements/link.rs b/src/elements/link.rs index cac621c..d76db5d 100644 --- a/src/elements/link.rs +++ b/src/elements/link.rs @@ -216,7 +216,7 @@ impl RegexRule for LinkRule { #[cfg(test)] mod tests { use crate::elements::style::Style; -use crate::elements::text::Text; + use crate::elements::text::Text; use crate::parser::langparser::LangParser; use crate::parser::source::SourceFile; use crate::validate_document; diff --git a/src/elements/list.rs b/src/elements/list.rs index 8a6397d..894fa33 100644 --- a/src/elements/list.rs +++ b/src/elements/list.rs @@ -1,235 +1,251 @@ -use std::{any::Any, cell::Ref, ops::Range, rc::Rc}; +use std::any::Any; +use std::cell::Ref; +use std::collections::HashMap; +use std::ops::Range; +use std::rc::Rc; -use crate::{compiler::compiler::{Compiler, Target}, document::{document::{Document, DocumentAccessors}, element::{ElemKind, Element}}, parser::{parser::Parser, rule::Rule, source::{Cursor, Source, Token, VirtualSource}}}; -use ariadne::{Label, Report, ReportKind}; -use mlua::{Function, Lua}; +use crate::compiler::compiler::Compiler; +use crate::compiler::compiler::Target; +use crate::document::document::Document; +use crate::document::document::DocumentAccessors; +use crate::document::element::ContainerElement; +use crate::document::element::ElemKind; +use crate::document::element::Element; +use crate::parser::parser::Parser; +use crate::parser::rule::Rule; +use crate::parser::source::Cursor; +use crate::parser::source::Source; +use crate::parser::source::Token; +use crate::parser::source::VirtualSource; +use crate::parser::util; +use crate::parser::util::process_escaped; +use crate::parser::util::Property; +use crate::parser::util::PropertyMapError; +use crate::parser::util::PropertyParser; +use ariadne::Label; +use ariadne::Report; +use ariadne::ReportKind; +use mlua::Function; +use mlua::Lua; +use regex::Match; use regex::Regex; -use super::paragraph::Paragraph; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MarkerKind { + Open, + Close, +} + +#[derive(Debug)] +pub struct ListMarker { + pub(self) location: Token, + pub(self) numbered: bool, + pub(self) kind: MarkerKind, +} + +impl Element for ListMarker { + fn location(&self) -> &Token { &self.location } + + fn kind(&self) -> ElemKind { ElemKind::Block } + + fn element_name(&self) -> &'static str { "List Marker" } + + fn to_string(&self) -> String { format!("{self:#?}") } + + fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result { + match compiler.target() { + Target::HTML => match (self.kind, self.numbered) { + (MarkerKind::Close, true) => Ok("".to_string()), + (MarkerKind::Close, false) => Ok("".to_string()), + (MarkerKind::Open, true) => Ok("
    ".to_string()), + (MarkerKind::Open, false) => Ok("
      ".to_string()), + }, + _ => todo!(), + } + } +} #[derive(Debug)] pub struct ListEntry { - location: Token, - numbering: Vec<(bool, usize)>, - content: Vec>, - - // TODO bullet_maker : FnMut<...> + pub(self) location: Token, + pub(self) numbering: Vec<(bool, usize)>, + pub(self) content: Vec>, + pub(self) bullet: Option, } -impl ListEntry { - pub fn new(location: Token, numbering: Vec<(bool, usize)>, content: Vec>) -> Self { - Self { location, numbering, content } - } -} - -#[derive(Debug)] -pub struct List -{ - location: Token, - entries: Vec -} - -impl List -{ - pub fn new(location: Token) -> Self - { - Self - { - location, - entries: Vec::new() - } - } - - pub fn push(&mut self, entry: ListEntry) - { - self.location.range = self.location.start()..entry.location.end(); - self.entries.push(entry); - } -} - -impl Element for List -{ - fn location(&self) -> &Token { &self.location } - - fn kind(&self) -> ElemKind { ElemKind::Block } - - fn element_name(&self) -> &'static str { "List" } - - fn to_string(&self) -> String { format!("{self:#?}") } - - fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result { - match compiler.target() - { - Target::HTML => { - let mut result = String::new(); - - //TODO: Do something about indexing - let mut current_list: Vec = vec![]; - let mut match_stack = |result: &mut String, target: &Vec<(bool, usize)>| { - - // Find index after which current_list and target differ - let mut match_idx = 0usize; - for i in 0..current_list.len() - { - if i >= target.len() || current_list[i] != target[i].0 { break } - else { match_idx = i+1; } - } - - // Close until same match - for _ in match_idx..current_list.len() - { - result.push_str(["
    ", "
"][current_list.pop().unwrap() as usize]); - } - - // Open - for i in match_idx..target.len() - { - result.push_str(["