Skip to content

Commit

Permalink
adding rows
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed May 10, 2024
1 parent 370757e commit 36079db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 97 deletions.
130 changes: 52 additions & 78 deletions src/interpreter/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use crate::interpreter::html::style::{FontStyle, FontWeight, Style, TextDecorati
use crate::interpreter::html::{style, Attr, HeaderType, Picture, TagName};
use crate::interpreter::{Span, WindowInteractor};
use crate::opts::ResolvedTheme;
use crate::positioner::{Positioned, Section, Spacer, DEFAULT_MARGIN};
use crate::positioner::{Positioned, Section, Spacer, DEFAULT_MARGIN, Row};
use crate::table::Table;
use crate::text::{Text, TextBox};
use crate::utils::{Align, ImageCache};
use crate::Element;
use comrak::Anchorizer;
use glyphon::FamilyOwned;
use parking_lot::Mutex;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use wgpu::TextureFormat;
Expand Down Expand Up @@ -67,51 +66,19 @@ impl<'a> Input<'a> {
}
type Opts<'a> = &'a AstOpts;

trait OutputStream {
type Output;
fn push(&mut self, i: impl Into<Self::Output>);
type Output<T> = Vec<T>;

fn map<F, O>(&mut self, f: F) -> Map<Self, F, O>
where
Self: Sized,
{
Map(self, f, PhantomData)
}
}
impl<T> OutputStream for Vec<T> {
type Output = T;
fn push(&mut self, i: impl Into<Self::Output>) {
self.push(i.into());
}
}
struct Map<'a, T: OutputStream, F, O>(&'a mut T, F, PhantomData<O>);
impl<'a, T, F, O> OutputStream for Map<'a, T, F, O>
where
T: OutputStream,
F: FnMut(O) -> T::Output,
{
type Output = O;
fn push(&mut self, i: impl Into<Self::Output>) {
self.0.push(self.1(i.into()))
}
}
struct Dummy<T>(PhantomData<T>);
impl<T> Dummy<T> {
const fn new() -> Self {
Self(PhantomData)
}
}
impl<T> OutputStream for Dummy<T> {
type Output = T;
fn push(&mut self, _i: impl Into<Self::Output>) {}
}
trait Push {
trait Push<T> {
fn push_element<I: Into<T>>(&mut self, element: I);
fn push_spacer(&mut self);
fn push_text_box(&mut self, global: &Static, element: &mut TextBox, state: State);
}
impl<T: OutputStream<Output = Element>> Push for T {
impl Push<Element> for Output<Element> {
fn push_element<I: Into<Element>>(&mut self, element: I) {
self.push(element.into());
}
fn push_spacer(&mut self) {
self.push(Spacer::invisible())
self.push_element(Spacer::invisible())
}
fn push_text_box(&mut self, global: &Static, element: &mut TextBox, state: State) {
let mut tb = std::mem::replace(element, TextBox::new(vec![], global.opts.hidpi_scale));
Expand All @@ -122,7 +89,7 @@ impl<T: OutputStream<Output = Element>> Push for T {

if content {
tb.indent = state.global_indent;
self.push(tb);
self.push_element(tb);
}
} else {
element.is_checkbox = tb.is_checkbox;
Expand Down Expand Up @@ -251,14 +218,14 @@ trait Process {
element: Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
);
fn process_content<'a>(
_global: &Static,
_element: Self::Context<'_>,
_state: State,
_input: impl IntoIterator<Item = &'a TextOrHirNode>,
_output: &mut impl OutputStream<Output = Element>,
_output: &mut Output<Element>,
) {
unimplemented!()
}
Expand Down Expand Up @@ -363,7 +330,7 @@ impl Process for FlowProcess {
element: Self::Context<'_>,
mut state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let attributes = &node.attributes;
match node.tag {
Expand Down Expand Up @@ -472,7 +439,7 @@ impl Process for FlowProcess {
output.push_text_box(global, element, state);
output.push_spacer();
}
TagName::HorizontalRuler => output.push(Spacer::visible()),
TagName::HorizontalRuler => output.push_element(Spacer::visible()),
TagName::Picture => PictureProcess::process(global, (), state, node, output),
TagName::Source => tracing::warn!("Source tag can only be inside an Picture."),
TagName::Image => ImageProcess::process(global, None, state, node, output),
Expand Down Expand Up @@ -577,7 +544,7 @@ impl Process for FlowProcess {
element: Self::Context<'_>,
state: State,
content: impl IntoIterator<Item = &'a TextOrHirNode>,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
for node in content {
match node {
Expand All @@ -604,7 +571,7 @@ impl Process for DetailsProcess {
_element: Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let mut section = Section::bare(global.opts.hidpi_scale);
*section.hidden.get_mut() = true;
Expand All @@ -626,7 +593,7 @@ impl Process for DetailsProcess {
&mut tb,
state.borrow(),
&summary.content,
&mut Dummy::new(),
&mut vec![],
);

*section.summary = Some(Positioned::new(tb));
Expand All @@ -638,14 +605,13 @@ impl Process for DetailsProcess {
}
}

let mut section_content = vec![];
let s = &mut section_content.map(Positioned::new);
let mut section_content: Vec<Element> = vec![];
let mut tb = TextBox::new(vec![], global.opts.hidpi_scale);

FlowProcess::process_content(global, &mut tb, state.borrow(), content, s);
s.push_text_box(global, &mut tb, state);
section.elements = section_content;
output.push(section)
FlowProcess::process_content(global, &mut tb, state.borrow(), content, &mut section_content);
section_content.push_text_box(global, &mut tb, state);
section.elements = section_content.drain(..).map(Positioned::new).collect();
output.push_element(section)
}
}

Expand All @@ -657,7 +623,7 @@ impl Process for OrderedListProcess {
element: Self::Context<'_>,
mut state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let mut index = 1;
for attr in &node.attributes {
Expand Down Expand Up @@ -699,7 +665,7 @@ impl Process for UnorderedListProcess {
element: Self::Context<'_>,
mut state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
output.push_text_box(global, element, state.borrow());
state.global_indent += DEFAULT_MARGIN / 2.;
Expand Down Expand Up @@ -728,7 +694,7 @@ impl Process for ListItemProcess {
(element, prefix): Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let anchor = node.attributes.iter().find_map(|attr| attr.to_anchor());
if let Some(anchor) = anchor {
Expand Down Expand Up @@ -769,21 +735,21 @@ impl Process for ListItemProcess {
struct ImageProcess;
impl ImageProcess {
fn push_image_from_picture(
opts: Opts,
global: &Static,
state: State,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
picture: Picture,
) {
let align = picture.inner.align;
let src = picture.resolve_src(opts.color_scheme).to_owned();
let src = picture.resolve_src(global.opts.color_scheme).to_owned();
let align = align.unwrap_or_default();
let is_url = src.starts_with("http://") || src.starts_with("https://");
let mut image = match opts.image_cache.lock().unwrap().get(&src) {
let mut image = match global.opts.image_cache.lock().unwrap().get(&src) {
Some(image_data) if is_url => {
Image::from_image_data(image_data.clone(), opts.hidpi_scale)
Image::from_image_data(image_data.clone(), global.opts.hidpi_scale)
}
_ => {
Image::from_src(src, opts.hidpi_scale, opts.window.lock().image_callback()).unwrap()
Image::from_src(src, global.opts.hidpi_scale, global.opts.window.lock().image_callback()).unwrap()
}
}
.with_align(align);
Expand All @@ -794,9 +760,17 @@ impl ImageProcess {
if let Some(size) = picture.inner.size {
image = image.with_size(size);
}

output.push(image);
output.push_spacer()

if Align::Left == align {
if let Some(Element::Row(row)) = output.iter_mut().next_back() {
row.elements.push(Positioned::new(image))
} else {
output.push_element(Row::with_image(image, global.opts.hidpi_scale))
}
} else {
output.push_element(image);
output.push_spacer()
}
}
}
impl Process for ImageProcess {
Expand All @@ -806,7 +780,7 @@ impl Process for ImageProcess {
mut element: Self::Context<'_>,
mut state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
if element.is_none() {
element = Some(Picture::builder());
Expand All @@ -829,7 +803,7 @@ impl Process for ImageProcess {
}

match builder.try_finish() {
Ok(pic) => Self::push_image_from_picture(global.opts, state, output, pic),
Ok(pic) => Self::push_image_from_picture(global, state, output, pic),
Err(err) => tracing::warn!("Invalid <img>: {err}"),
}
}
Expand All @@ -842,7 +816,7 @@ impl Process for SourceProcess {
element: Self::Context<'_>,
_state: State,
node: &HirNode,
_output: &mut impl OutputStream<Output = Element>,
_output: &mut Output<Element>,
) {
let mut media = None;
let mut src_set = None;
Expand Down Expand Up @@ -873,7 +847,7 @@ impl Process for PictureProcess {
_element: Self::Context<'_>,
mut state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let mut builder = Picture::builder();

Expand Down Expand Up @@ -914,7 +888,7 @@ impl Process for TableProcess {
_element: Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
let mut table = Table::new();
Self::process_with(
Expand All @@ -935,7 +909,7 @@ impl Process for TableProcess {
|_| {},
);
output.push_spacer();
output.push(table);
output.push_element(table);
output.push_spacer();
}
}
Expand All @@ -948,7 +922,7 @@ impl Process for TableHeadProcess {
element: Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
Self::process_with(
global,
Expand Down Expand Up @@ -977,7 +951,7 @@ impl Process for TableRowProcess {
element: Self::Context<'_>,
state: State,
node: &HirNode,
output: &mut impl OutputStream<Output = Element>,
output: &mut Output<Element>,
) {
Self::process_with(
global,
Expand Down Expand Up @@ -1014,7 +988,7 @@ impl Process for TableCellProcess {
(table, is_header): Self::Context<'_>,
mut state: State,
node: &HirNode,
_output: &mut impl OutputStream<Output = Element>,
_output: &mut Output<Element>,
) {
let row = table
.rows
Expand All @@ -1032,7 +1006,7 @@ impl Process for TableCellProcess {
&mut tb,
state,
&node.content,
&mut Dummy::new(), // TODO allow anything inside tables not only text.
&mut vec![], // TODO allow anything inside tables not only text.
);

row.push(tb);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
---
source: src/interpreter/tests.rs
description: "![This actually returns JSON 😈](http://127.0.0.1:43275/snapshot.png)"
description: "![This actually returns JSON 😈](http://127.0.0.1:41497/snapshot.png)"
expression: "interpret_md_with_opts(&text, opts)"
---
[
Image(
Image {
image_data: Mutex {
data: Some(
ImageData {
lz4_blob: { len: 7759, data: [4, 34, 77, ..] },
scale: false,
dimensions: (63, 72),
},
),
poisoned: false,
..
},
is_aligned: Some(Left),
..
Row(
Row {
elements: [
Positioned {
inner: Image(
Image {
image_data: Mutex {
data: Some(
ImageData {
lz4_blob: { len: 7759, data: [4, 34, 77, ..] },
scale: false,
dimensions: (63, 72),
},
),
poisoned: false,
..
},
is_aligned: Some(Left),
..
},
),
bounds: None,
},
],
hidpi_scale: 1.0,
},
),
Spacer(
InvisibleSpacer(5),
),
Spacer(
InvisibleSpacer(5),
),
]

0 comments on commit 36079db

Please sign in to comment.