Skip to content

Commit

Permalink
WIP: Removing a lot of extra code in anticipation of kivikakk/comrak#494
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpeach committed Dec 17, 2024
1 parent 99d8eca commit ca2324e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ debug = true
aho-corasick = "1.1.3"
bon = "2.3.0"
clap = { version = "4.5.16", features = ["derive"] }
comrak = "0.29.0"
comrak = "0.31.0"
derive_more = { version = "1.0.0", features = ["full"] }
env_logger = "0.11.5"
fuzzy-matcher = "0.3.7"
Expand Down
49 changes: 0 additions & 49 deletions src/file/content/front_matter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use comrak::{
arena_tree::Node,
nodes::{Ast, NodeValue},
};
use log::debug;
use miette::SourceSpan;
use serde::Deserialize;

use super::wikilink::Alias;
Expand All @@ -34,53 +32,6 @@ impl FrontMatterVisitor {
}
}

fn get_frontmatter_from_any_node(node: &Node<RefCell<Ast>>) -> Option<String> {
let mut node = node; // Its not the node which is now mutable, but you can change this value
// First check ourselves
if let NodeValue::FrontMatter(text) = &node.data.borrow().value {
return Some(text.clone());
}

// Then go to the root "Document" node
while let Some(this_node) = node.parent() {
node = this_node;
if let NodeValue::Document = node.data.borrow().value {
break;
}
}

// Down one is elther NodeValue::FrontMatter or Something else
// If its frontmatter, return it
if let Some(child) = &node.first_child() {
if let NodeValue::FrontMatter(text) = &child.data.borrow().value {
return Some(text.clone());
}
}
None
}

/// Spans get messed up because frontmatter is not considered part of the nodes sourcespan
/// This adds the frontmatter length to the span offset
pub fn repair_span_due_to_frontmatter(span: SourceSpan, node: &Node<RefCell<Ast>>) -> SourceSpan {
let frontmatter = get_frontmatter_from_any_node(node);
if let Some(frontmatter) = frontmatter {
debug!("Frontmatter: {:?}", frontmatter);
SourceSpan::new((span.offset() + frontmatter.len()).into(), span.len())
} else {
span
}
}

/// Remove the frontmatter from the source
pub fn remove_frontmatter_from_source(source: &str, node: &Node<RefCell<Ast>>) -> String {
let frontmatter = get_frontmatter_from_any_node(node);
if let Some(frontmatter) = frontmatter {
source[frontmatter.len()..].to_string()
} else {
source.to_string()
}
}

impl Visitor for FrontMatterVisitor {
fn name(&self) -> &'static str {
"FrontMatterVisitor"
Expand Down
36 changes: 11 additions & 25 deletions src/file/content/wikilink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use comrak::{
use miette::{SourceOffset, SourceSpan};
use regex::Regex;

use super::front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter};

/// A linkable string, like that in a wikilink, or its corresponding filename
/// Aliases are always lowercase
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -108,24 +106,15 @@ impl Visitor for WikilinkVisitor {
.get(1)
.expect("The regex has 2 capture groups")
.start();
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
let sourcepos_start_offset_bytes = SourceOffset::from_location(
text_without_frontmatter,
sourcepos.start.line,
sourcepos.start.column,
)
.offset();
let sourcepos_start_offset_bytes =
SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column)
.offset();
let span = SourceSpan::new(
(sourcepos_start_offset_bytes + capture_start_byte).into(),
alias.char_len(),
);
let span_repaired = repair_span_due_to_frontmatter(span, node);
self.wikilinks.push(
Wikilink::builder()
.alias(alias.clone())
.span(span_repaired)
.build(),
);
self.wikilinks
.push(Wikilink::builder().alias(alias.clone()).span(span).build());
}
};
match data {
Expand All @@ -136,16 +125,13 @@ impl Visitor for WikilinkVisitor {
self.wikilinks.push(
Wikilink::builder()
.alias(Alias::new(url))
.span(repair_span_due_to_frontmatter(
SourceSpan::new(
SourceOffset::from_location(
remove_frontmatter_from_source(source, node),
sourcepos.start.line,
sourcepos.start.column,
),
url.len() + 4,
.span(SourceSpan::new(
SourceOffset::from_location(
source,
sourcepos.start.line,
sourcepos.start.column,
),
node,
url.len() + 4,
))
.build(),
);
Expand Down
18 changes: 5 additions & 13 deletions src/rules/unlinked_text.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
config::Config,
file::{
content::{
front_matter::{remove_frontmatter_from_source, repair_span_due_to_frontmatter},
wikilink::{Alias, WikilinkVisitor},
},
content::wikilink::{Alias, WikilinkVisitor},
name::{get_filename, Filename},
},
sed::ReplacePair,
Expand Down Expand Up @@ -184,17 +181,12 @@ impl Visitor for UnlinkedTextVisitor {
if "lorem" == alias.to_string() {
println!("Found lorem");
}
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
let sourcepos_start_offset_bytes = SourceOffset::from_location(
text_without_frontmatter,
sourcepos.start.line,
sourcepos.start.column,
)
.offset();
let sourcepos_start_offset_bytes =
SourceOffset::from_location(text, sourcepos.start.line, sourcepos.start.column)
.offset();
let byte_length = found.end() - found.start();
let offset_bytes = sourcepos_start_offset_bytes + found.start();
let span = SourceSpan::new(offset_bytes.into(), byte_length);
let span_repaired = repair_span_due_to_frontmatter(span, node);

// Dont match inside wikilinks
if let Some(parent) = parent {
Expand All @@ -204,7 +196,7 @@ impl Visitor for UnlinkedTextVisitor {
}
}

self.new_unlinked_texts.push((alias, span_repaired));
self.new_unlinked_texts.push((alias, span));
}
}
Ok(())
Expand Down
11 changes: 4 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use std::{
rc::Rc,
};

use comrak::{
arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptionsBuilder, Options,
};
use comrak::{arena_tree::Node, nodes::Ast, parse_document, Arena, ExtensionOptions, Options};
use log::{debug, trace};
use std::backtrace;
use thiserror::Error;
Expand Down Expand Up @@ -135,11 +133,10 @@ pub fn parse(path: &PathBuf, visitors: Vec<Rc<RefCell<dyn Visitor>>>) -> Result<

// Parse the source code
let arena = Arena::new();
let options = ExtensionOptionsBuilder::default()
.front_matter_delimiter(Some("---".to_string()))
let options = ExtensionOptions::builder()
.front_matter_delimiter("---".to_string())
.wikilinks_title_before_pipe(true)
.build()
.expect("Constant");
.build();
let root = parse_document(
&arena,
&source,
Expand Down

0 comments on commit ca2324e

Please sign in to comment.