Skip to content

Commit

Permalink
Merge pull request #46 from jaredh159/lit-mono-img-title
Browse files Browse the repository at this point in the history
2 bugfixes
  • Loading branch information
jaredh159 authored Feb 10, 2025
2 parents 678559c + 8f0cdf0 commit d2453c2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 58 deletions.
2 changes: 1 addition & 1 deletion backend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait Backend {
fn enter_passthrough_block(&mut self, block: &Block, content: &BlockContent);
fn exit_passthrough_block(&mut self, block: &Block, content: &BlockContent);
fn enter_image_block(&mut self, img_target: &str, img_attrs: &AttrList, block: &Block);
fn exit_image_block(&mut self, block: &Block);
fn exit_image_block(&mut self, img_target: &str, img_attrs: &AttrList, block: &Block);
fn enter_admonition_block(&mut self, kind: AdmonitionKind, block: &Block);
fn exit_admonition_block(&mut self, kind: AdmonitionKind, block: &Block);
fn enter_quoted_paragraph(&mut self, block: &Block, attr: &str, cite: Option<&str>);
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,5 @@ impl DiagnosticColor for Colorizer {
format!("{}", Into::<String>::into(s).red().bold())
}
}

// hack: force cli version publish - dec4b426
81 changes: 43 additions & 38 deletions dr-html-backend/src/asciidoctor_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Backend for AsciidoctorHtml {
if self.render_doc_title() {
self.push_str("</h1>");
} else {
self.take_buffer(); // discard
self.swap_take_buffer(); // discard
}
self.render_document_authors();
}
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Backend for AsciidoctorHtml {
#[instrument(skip_all)]
fn enter_listing_block(&mut self, block: &Block, _content: &BlockContent) {
self.open_element("div", &["listingblock"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_str(r#"<div class="content"><pre"#);
if let Some(lang) = self.source_lang(block) {
self.push([
Expand Down Expand Up @@ -355,7 +355,7 @@ impl Backend for AsciidoctorHtml {
#[instrument(skip_all)]
fn enter_quoted_paragraph(&mut self, block: &Block, _attr: &str, _cite: Option<&str>) {
self.open_element("div", &["quoteblock"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_str("<blockquote>");
}

Expand All @@ -367,7 +367,7 @@ impl Backend for AsciidoctorHtml {
#[instrument(skip_all)]
fn enter_quote_block(&mut self, block: &Block, _content: &BlockContent) {
self.open_element("div", &["quoteblock"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_str("<blockquote>");
}

Expand All @@ -383,7 +383,7 @@ impl Backend for AsciidoctorHtml {
#[instrument(skip_all)]
fn enter_verse_block(&mut self, block: &Block, _content: &BlockContent) {
self.open_element("div", &["verseblock"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_str(r#"<pre class="content">"#);
}

Expand Down Expand Up @@ -477,7 +477,7 @@ impl Backend for AsciidoctorHtml {
ul.push_class("checklist");
}
self.push_open_tag(div);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_open_tag(ul);
}

Expand Down Expand Up @@ -506,7 +506,7 @@ impl Backend for AsciidoctorHtml {
#[instrument(skip_all)]
fn enter_description_list(&mut self, block: &Block, _items: &[ListItem], _depth: u8) {
self.open_element("div", &["dlist"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push_str("<dl>");
}

Expand Down Expand Up @@ -561,7 +561,7 @@ impl Backend for AsciidoctorHtml {
let class = custom.unwrap_or_else(|| list_class_from_depth(depth));
let classes = &["olist", class];
self.open_element("div", classes, &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
self.push([r#"<ol class=""#, class, "\""]);

if list_type != "1" {
Expand Down Expand Up @@ -639,7 +639,7 @@ impl Backend for AsciidoctorHtml {
if self.doc_meta.get_doctype() != DocType::Inline {
if !self.state.contains(&VisitingSimpleTermDescription) {
self.open_element("div", &["paragraph"], &block.meta.attrs);
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
}
self.push_str("<p>");
}
Expand Down Expand Up @@ -999,7 +999,7 @@ impl Backend for AsciidoctorHtml {
false
};

self.render_image(target, attrs);
self.render_image(target, attrs, false);
if with_link {
self.push_str("</a>");
}
Expand Down Expand Up @@ -1212,7 +1212,7 @@ impl Backend for AsciidoctorHtml {
self.push([kind.str(), r#""></i></td><td class="content">"#]);
}
}
self.render_block_title(&block.meta);
self.render_buffered_block_title(&block.meta);
}

#[instrument(skip_all)]
Expand All @@ -1239,22 +1239,27 @@ impl Backend for AsciidoctorHtml {
self.push([r#"<a class="image" href=""#, *href, r#"">"#]);
has_link = true;
}
self.render_image(img_target, img_attrs);
self.render_image(img_target, img_attrs, true);
if has_link {
self.push_str("</a>");
}
self.push_str(r#"</div>"#);
}

#[instrument(skip_all)]
fn exit_image_block(&mut self, block: &Block) {
fn exit_image_block(&mut self, _target: &str, attrs: &AttrList, block: &Block) {
let prefix = if self.doc_meta.is_false("figure-caption") {
None
} else {
self.fig_caption_num += 1;
Some(Cow::Owned(format!("Figure {}. ", self.fig_caption_num)))
};
self.render_prefixed_block_title(&block.meta, prefix);
if let Some(title) = attrs.named("title") {
self.render_block_title(title, prefix);
} else if block.meta.title.is_some() {
let title = self.take_buffer();
self.render_block_title(&title, prefix);
}
self.push_str(r#"</div>"#);
}

Expand Down Expand Up @@ -1306,7 +1311,7 @@ impl Backend for AsciidoctorHtml {
return; // this means the footnore was referring to a previously defined fn by id
}
let num = self.footnotes.borrow().len() + 1;
let footnote = self.take_buffer();
let footnote = self.swap_take_buffer();
let nums = num.to_string();
self.push_str(r#"<sup class="footnote""#);
if let Some(id) = id {
Expand Down Expand Up @@ -1346,11 +1351,19 @@ impl AsciidoctorHtml {
}

pub(crate) fn push_buffered(&mut self) {
let mut buffer = String::new();
mem::swap(&mut buffer, &mut self.alt_html);
let buffer = self.take_buffer();
self.push_str(&buffer);
}

fn take_buffer(&mut self) -> String {
mem::take(&mut self.alt_html)
}

fn swap_take_buffer(&mut self) -> String {
std::mem::swap(&mut self.alt_html, &mut self.html);
std::mem::take(&mut self.alt_html)
}

pub(crate) fn push_open_tag(&mut self, tag: OpenTag) {
self.push_str(&tag.finish());
}
Expand All @@ -1368,23 +1381,20 @@ impl AsciidoctorHtml {
}
}

fn render_block_title(&mut self, meta: &ChunkMeta) {
fn render_buffered_block_title(&mut self, meta: &ChunkMeta) {
if meta.title.is_some() {
self.push_str(r#"<div class="title">"#);
self.push_buffered();
self.push_str("</div>");
let buf = self.take_buffer();
self.render_block_title(&buf, None);
}
}

fn render_prefixed_block_title(&mut self, meta: &ChunkMeta, prefix: Option<Cow<str>>) {
if meta.title.is_some() {
self.push_str(r#"<div class="title">"#);
if let Some(prefix) = prefix {
self.push_str(&prefix);
}
self.push_buffered();
self.push_str("</div>");
fn render_block_title(&mut self, title: &str, prefix: Option<Cow<str>>) {
self.push_str(r#"<div class="title">"#);
if let Some(prefix) = prefix {
self.push_str(&prefix);
}
self.push_str(title);
self.push_str("</div>");
}

pub(crate) fn open_element(&mut self, element: &str, classes: &[&str], attrs: &impl AttrData) {
Expand Down Expand Up @@ -1494,13 +1504,6 @@ impl AsciidoctorHtml {
mem::swap(&mut self.html, &mut self.alt_html);
}

fn take_buffer(&mut self) -> String {
mem::swap(&mut self.alt_html, &mut self.html);
let mut buffered = String::new();
mem::swap(&mut buffered, &mut self.alt_html);
buffered
}

// TODO: handle embedding images, data-uri, etc., this is a naive impl
// @see https://github.com/jaredh159/asciidork/issues/7
fn push_icon_uri(&mut self, name: &str, prefix: Option<&str>) {
Expand Down Expand Up @@ -1596,7 +1599,7 @@ impl AsciidoctorHtml {
self.push_str("</object>");
}

fn render_image(&mut self, target: &str, attrs: &AttrList) {
fn render_image(&mut self, target: &str, attrs: &AttrList, is_block: bool) {
let format = attrs.named("format").or_else(|| file::ext(target));
let is_svg = matches!(format, Some("svg" | "SVG"));
if is_svg && attrs.has_option("interactive") && self.doc_meta.safe_mode != SafeMode::Secure {
Expand All @@ -1618,7 +1621,9 @@ impl AsciidoctorHtml {
self.push_ch('"');
self.push_named_or_pos_attr("width", 1, attrs);
self.push_named_or_pos_attr("height", 2, attrs);
self.push_named_attr("title", attrs);
if !is_block {
self.push_named_attr("title", attrs);
}
self.push_ch('>');
}
}
Expand Down
15 changes: 15 additions & 0 deletions dr-html-backend/tests/all/eval_image_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ assert_html!(
"#}
);

assert_html!(
block_image_title_rendered_below,
adoc! {r#"
image::flower.jpg[title="So pretty"]
"#},
html! {r#"
<div class="imageblock">
<div class="content">
<img src="flower.jpg" alt="flower">
</div>
<div class="title">Figure 1. So pretty</div>
</div>
"#}
);

// https://docs.asciidoctor.org/asciidoc/latest/macros/image-position/
assert_html!(
image_size,
Expand Down
2 changes: 1 addition & 1 deletion eval/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn eval_block(block: &Block, ctx: &Ctx, backend: &mut impl Backend) {
}
(Context::Image, Content::Empty(EmptyMetadata::Image { target, attrs })) => {
backend.enter_image_block(target, attrs, block);
backend.exit_image_block(block);
backend.exit_image_block(target, attrs, block);
}
(Context::DocumentAttributeDecl, Content::DocumentAttribute(name, entry)) => {
backend.visit_document_attribute_decl(name, entry);
Expand Down
4 changes: 2 additions & 2 deletions parser/src/tasks/parse_inlines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<'arena> Parser<'arena> {

Backtick
if subs.inline_formatting()
&& line.current_is(Plus)
&& line.current_token().is_kind_len(Plus, 1)
&& contains_seq(&[Len(1, Plus), Kind(Backtick)], &line, lines) =>
{
self.ctx.subs.remove(Subs::InlineFormatting);
Expand Down Expand Up @@ -557,7 +557,7 @@ impl<'arena> Parser<'arena> {

Backtick
if subs.inline_formatting()
&& self.starts_unconstrained(&[Kind(Backtick)], &token, &line, lines) =>
&& self.starts_unconstrained(&[Kind(Backtick); 2], &token, &line, lines) =>
{
self.parse_node(Mono, [Kind(Backtick); 2], &token, &mut acc, line, lines)?;
break;
Expand Down
46 changes: 30 additions & 16 deletions parser/tests/all/parse_inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,6 @@ fn test_joining_newlines() {
node!("bar"; 6..9),
],
),
(
"`+{name}+`\nbar",
nodes![
node!(LitMono(src!("{name}", 2..8)), 0..10),
node!(Inline::Newline, 10..11),
node!("bar"; 11..14),
],
),
(
"+_foo_+\nbar",
nodes![
Expand Down Expand Up @@ -453,6 +445,35 @@ fn test_line_breaks() {
]);
}

#[test]
fn test_lit_mono() {
run(vec![
(
"`+{name}+`\nbar",
nodes![
node!(LitMono(src!("{name}", 2..8)), 0..10),
node!(Inline::Newline, 10..11),
node!("bar"; 11..14),
],
),
(
"`+{name}+`",
nodes![node!(LitMono(src!("{name}", 2..8)), 0..10)],
),
(
"`+_foo_+`",
nodes![node!(LitMono(src!("_foo_", 2..7)), 0..9)],
),
(
"`++f` bar +` baz", // <-- NOT lit mono
nodes![
node!(Mono(just!("++f", 1..4)), 0..5),
node!(" bar +` baz"; 5..16),
],
),
]);
}

#[test]
fn test_inline_anchors() {
run(vec![
Expand Down Expand Up @@ -523,6 +544,7 @@ fn test_mono() {
node!(Mono(nodes![node!("'bar'"; 5..10)]), 4..11),
],
),
("`not`mono", just!("`not`mono", 0..9)),
]);
}

Expand Down Expand Up @@ -834,14 +856,6 @@ fn test_parse_inlines() {
node!("bar"; 6..9),
],
),
(
"`+{name}+`",
nodes![node!(LitMono(src!("{name}", 2..8)), 0..10)],
),
(
"`+_foo_+`",
nodes![node!(LitMono(src!("_foo_", 2..7)), 0..9)],
),
(
"foo <bar> & lol",
nodes![
Expand Down
Binary file modified web-playground/public/wasm/dr_html_wasm_bg.wasm
Binary file not shown.

0 comments on commit d2453c2

Please sign in to comment.