Skip to content

Commit

Permalink
refactor: Update TextOptions struct to include link field.
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Apr 22, 2024
1 parent 29318c5 commit 55b082f
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions src/interpreter/ast.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
//#[derive(SmartDebug, Clone)]
//pub struct HirNode {
// #[debug(skip)]
// pub parent: crate::interpreter::hir::WeakNode,
// pub tag: TagName,
// pub attributes: Vec<Attr>,
// pub content: Vec<TextOrHirNode>,
//}

use std::cell::{Cell, Ref, RefCell};
use crate::color::{native_color, Theme};
use crate::interpreter::hir::{unwrap_hir_node, Hir, HirNode, TextOrHirNode};
Expand All @@ -21,12 +12,13 @@ use crate::{table, Element};
use comrak::Anchorizer;
use glyphon::FamilyOwned;
use std::collections::VecDeque;
use std::num::NonZeroU8;
use std::num::{NonZeroU8, NonZeroUsize};
use std::ops::DerefMut;
use html5ever::tendril::{SliceExt, StrTendril};
use lyon::geom::utils::tangent;
use wgpu::TextureFormat;

#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Clone, Default)]
struct TextOptions {
pub underline: bool,
pub bold: bool,
Expand All @@ -37,16 +29,17 @@ struct TextOptions {
pub pre_formatted: bool,
pub block_quote: u8,
pub align: Option<Align>,
pub link: Option<StrTendril>,
}

#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Clone, Default)]
struct InheritedState {
global_indent: f32,
text_options: TextOptions,
span: Span,

/// Li render as ether as "· " or as an "{1..}. ".
list_prefix: Option<Option<NonZeroU8>>,
list_prefix: Option<Option<NonZeroUsize>>,
}
impl InheritedState {
fn with_span_color(span_color: [f32; 4]) -> Self {
Expand All @@ -73,7 +66,6 @@ pub struct Ast {
pub current_textbox: RefCell<TextBox>,
pub hidpi_scale: f32,
pub surface_format: TextureFormat,
pub link: Cell<Option<String>>,
}
impl Ast {
pub fn new() -> Self {
Expand All @@ -84,7 +76,6 @@ impl Ast {
hidpi_scale: Default::default(),
theme: Theme::dark_default(),
surface_format: TextureFormat::Bgra8UnormSrgb,
link: Cell::new(None),
}
}
pub fn interpret(mut self, hir: Hir) -> Self {
Expand All @@ -101,9 +92,9 @@ impl Ast {
fn process_content(&mut self, inherited_state: InheritedState, content: Content) {
for node in content {
match node {
TextOrHirNode::Text(str) => self.text(self.current_textbox.borrow_mut().deref_mut(), inherited_state, str),
TextOrHirNode::Text(str) => self.text(self.current_textbox.borrow_mut().deref_mut(), inherited_state.clone(), str),
TextOrHirNode::Hir(node) => {
self.process_node(inherited_state, unwrap_hir_node(node))
self.process_node(inherited_state.clone(), unwrap_hir_node(node))
}
}
}
Expand All @@ -115,41 +106,44 @@ impl Ast {

match node.tag {
TagName::Paragraph => {
self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());

self.process_content(inherited_state, content);
self.process_content(inherited_state.clone(), content);

self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());
self.push_spacer();
}
TagName::Anchor => {
for attr in attributes {
match attr {
Attr::Href(link) => self.link.set(Some(link)),
Attr::Href(link) => inherited_state.text_options.link = Some(link.to_tendril()),
Attr::Anchor(a) => self.current_textbox.borrow_mut().set_anchor(a),
_ => {}
}
}
self.process_content(inherited_state, content);
}
TagName::Div => {
self.push_text_box(inherited_state);
self.process_content(inherited_state, content);
self.push_text_box(inherited_state.clone());
self.process_content(inherited_state.clone(), content);
self.push_text_box(inherited_state);
}
//_ => {
// tracing::warn!("Interpreter didn't implement {:?}", node.tag);
// return;
//}
TagName::BlockQuote => {
self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());
inherited_state.text_options.block_quote += 1;
inherited_state.global_indent += DEFAULT_MARGIN / 2.;

self.process_content(inherited_state, content);
self.process_content(inherited_state.clone(), content);

let indent = inherited_state.global_indent;

self.push_text_box(inherited_state);
if inherited_state.global_indent == DEFAULT_MARGIN / 2. {

if indent == DEFAULT_MARGIN / 2. {
self.push_spacer();
}
}
Expand All @@ -158,7 +152,7 @@ impl Ast {
self.process_content(inherited_state, content);
}
TagName::Break => {
self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());
self.process_content(inherited_state, content);
}
TagName::Code => {
Expand Down Expand Up @@ -188,7 +182,7 @@ impl Ast {
self.process_content(inherited_state, content);
}
TagName::Header(header) => {
self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());
self.push_spacer();

inherited_state.set_align(attributes.iter().find_map(|attr| attr.to_align()));
Expand All @@ -198,7 +192,7 @@ impl Ast {
if header == HeaderType::H1 {
inherited_state.text_options.underline = true;
}
self.process_content(inherited_state, content);
self.process_content(inherited_state.clone(), content);

let anchor = self
.current_textbox.borrow()
Expand Down Expand Up @@ -253,7 +247,7 @@ impl Ast {
self.process_unordered_list(inherited_state, content, attributes);
}
TagName::PreformattedText => {
self.push_text_box(inherited_state);
self.push_text_box(inherited_state.clone());
let style = attributes.iter().find_map(|attr| attr.to_style()).unwrap_or_default();
for style in style::Iter::new(&style) {
if let Style::BackgroundColor(color) = style {
Expand All @@ -263,12 +257,10 @@ impl Ast {
}
inherited_state.text_options.pre_formatted = true;
self.current_textbox.borrow_mut().set_code_block(true);
self.process_content(inherited_state, content);
self.process_content(inherited_state.clone(), content);

self.push_text_box(inherited_state);
self.push_spacer();
inherited_state.text_options.pre_formatted = false;
self.current_textbox.borrow_mut().set_code_block(false);
}
TagName::Small => {
inherited_state.text_options.small = true;
Expand Down Expand Up @@ -321,14 +313,11 @@ impl Ast {
inherited_state.text_options.underline = true;
self.process_content(inherited_state, content);
}
TagName::Root => {
tracing::error!("Root element can't reach interpreter.");
return;
}
TagName::Root => tracing::error!("Root element can't reach interpreter."),
}
}

fn text(&self, text_box: &mut TextBox, state: InheritedState, mut string: String){
fn text(&self, text_box: &mut TextBox, mut state: InheritedState, mut string: String){
let text_native_color = self.native_color(self.theme.text_color);
if string == "\n" {
if state.text_options.pre_formatted {
Expand Down Expand Up @@ -416,7 +405,7 @@ impl Ast {
// break;
// }
//}
if let Some(link) = self.link.take() {
if let Some(link) = state.text_options.link.take() {
text = text.with_link(link.to_string());
text = text.with_color(self.native_color(self.theme.link_color));
}
Expand Down Expand Up @@ -500,11 +489,11 @@ impl Ast {
|_| {},
|node| match node.tag {
TagName::TableHead | TagName::TableBody => {
self.process_table_head_body(table, inherited_state, node.content);
self.process_table_head_body(table, inherited_state.clone(), node.content);
}
TagName::TableRow => {
table.rows.push(vec![]);
self.process_table_row(table, inherited_state, node.content)
self.process_table_row(table, inherited_state.clone(), node.content)
}
_ => tracing::warn!("Only TableHead, TableBody, TableRow and TableFoot can be inside an table, found: {:?}", node.tag),
}
Expand All @@ -518,7 +507,7 @@ impl Ast {
|node| match node.tag {
TagName::TableRow => {
table.rows.push(vec![]);
self.process_table_row(table, inherited_state, node.content)
self.process_table_row(table, inherited_state.clone(), node.content)
}
_ => tracing::warn!("Only TableRows can be inside an TableHead or TableBody, found: {:?}", node.tag)
}
Expand All @@ -531,7 +520,7 @@ impl Ast {
content,
|_| {},
|node| {
let mut inherited_state = inherited_state;
let mut inherited_state = inherited_state.clone();
inherited_state.set_align(node.attributes.iter().find_map(|attr| attr.to_align()));
match node.tag {
TagName::TableHeader => self.process_table_header(table, inherited_state, node.content),
Expand All @@ -551,7 +540,7 @@ impl Ast {
content,
|text| {
let mut tb = TextBox::new(vec![], self.hidpi_scale);
self.text(&mut tb, inherited_state, text);
self.text(&mut tb, inherited_state.clone(), text);
row.push(tb);
},
|_| tracing::warn!("Currently only text is allowed in an TableHeader."),
Expand All @@ -568,13 +557,13 @@ impl Ast {
content,
|text| {
let mut tb = TextBox::new(vec![], self.hidpi_scale);
self.text(&mut tb, inherited_state, text);
self.text(&mut tb, inherited_state.clone(), text);
row.push(tb);
},
|_| tracing::warn!("Currently only text is allowed in an TableDataCell.")
);
}
fn process_ordered_list(&mut self, inherited_state: InheritedState, content: Content, attributes: Attributes) {
fn process_ordered_list(&mut self, mut inherited_state: InheritedState, content: Content, attributes: Attributes) {
let mut index = 1;
for attr in attributes {
if let Attr::Start(start) = attr {
Expand All @@ -587,23 +576,28 @@ impl Ast {
|_| {},
|node| match node.tag {
TagName::ListItem => {
self.process_list_item(format!("{index}. "), inherited_state, node.content, node.attributes);
inherited_state.list_prefix = Some(NonZeroUsize::try_from(index).ok());
self.process_list_item(inherited_state.clone(), node.content, node.attributes);
index += 1;
}
_ => tracing::warn!("Only ListItems can be inside an List"),
}
)
}
fn process_unordered_list(&mut self, inherited_state: InheritedState, content: Content, attributes: Attributes) {
fn process_unordered_list(&mut self, mut inherited_state: InheritedState, content: Content, attributes: Attributes) {
Self::process_node_content(content,
|_| {},
|node| match node.tag {
TagName::ListItem => self.process_list_item("· ".to_string(), inherited_state, node.content, node.attributes),
TagName::ListItem => {
inherited_state.list_prefix = Some(None);
self.process_list_item(inherited_state.clone(), node.content, node.attributes);
},
_ => tracing::warn!("Only ListItems can be inside an List"),
}
);
}
fn process_list_item(&mut self, prefix: String, inherited_state: InheritedState, content: Content, attributes: Attributes) {
fn process_list_item(&mut self, inherited_state: InheritedState, content: Content, attributes: Attributes) {
tracing::warn!("No li impl");
//let anchor = attributes.iter().find_map(|attr| attr.to_anchor());
//self.push_text_box(inherited_state);
//self.current_textbox.borrow_mut()
Expand Down

0 comments on commit 55b082f

Please sign in to comment.