-
Notifications
You must be signed in to change notification settings - Fork 1
Include Markdown and notebook files with scoped references #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PiotrCzapla
wants to merge
13
commits into
main
Choose a base branch
from
board-consent-packet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
46493bc
Frontmatter number_headings selects the heading numbering; refs='ids'…
PiotrCzapla 3dea461
Merge remote-tracking branch 'origin/main' into frontmatter-numbering
PiotrCzapla f9c7bb0
Simplify notebook loading and remove dialect additions
PiotrCzapla 062a973
Merge main and adapt frontmatter binding to Python crate
PiotrCzapla 2c2aeca
Scope included documents and preserve local heading schemes
PiotrCzapla 03ff142
Merge main into included-document export branch
PiotrCzapla d36c0a8
Prefix included IDs and use ordinary title numbering resets
PiotrCzapla 506e023
Resolve include scopes while producing MDHTML
PiotrCzapla 74771f8
Include Markdown and notebook files with template defaults
PiotrCzapla 06c81f1
Simplify include prefixes and caller-controlled layout
PiotrCzapla 0d33889
Correct documented reference ID grammar
PiotrCzapla c5f704e
Append include scopes as literal ID suffixes
PiotrCzapla 4a65c09
Keep reference character checks explicit
PiotrCzapla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| use std::collections::{HashMap, HashSet}; | ||
|
|
||
| use fast5ever::{DOCUMENT, parse_fragment}; | ||
| use crate::{Attr, Block, Diagnostic, Document}; | ||
|
|
||
| pub(crate) fn qualify(src: &str, suffix: Option<&str>) -> String { | ||
| if suffix.is_none() && !src.to_ascii_lowercase().contains("scope") { return src.into(); } | ||
| let mut dom = parse_fragment(src, "body"); | ||
| let mut includes: Vec<_> = dom.descendants(DOCUMENT).into_iter().filter_map(|e| { | ||
| if !dom.has_class(e, "include") { return None; } | ||
| dom.attr(e, "scope").map(|s| (e, s.to_string())) | ||
| }).collect(); | ||
| includes.reverse(); | ||
| if let Some(suffix) = suffix { includes.push((DOCUMENT, suffix.into())); } | ||
| if includes.is_empty() { return src.into(); } | ||
| for (inc, scope) in includes { | ||
| let _ = dom.remove_attr(inc, "scope"); | ||
| let els: Vec<_> = dom.descendants(inc).into_iter().skip(1).collect(); | ||
| let ids: HashMap<_, _> = els.iter().filter_map(|&e| { | ||
| dom.attr(e, "id").map(|id| (id.to_string(), format!("{id}{scope}"))) | ||
| }).collect(); | ||
| for e in els { | ||
| if let Some(id) = dom.attr(e, "id").and_then(|id| ids.get(id)) { let _ = dom.set_attr(e, "id", id); } | ||
| if let Some(id) = dom.attr(e, "href").and_then(|s| s.strip_prefix('#')).and_then(|id| ids.get(id)) { | ||
| let _ = dom.set_attr(e, "href", &format!("#{id}")); | ||
| } | ||
| } | ||
| } | ||
| dom.to_html(DOCUMENT) | ||
| } | ||
|
|
||
| pub(crate) fn validate(doc: &mut Document) { | ||
| fn check(scope: &str, seen: &mut HashSet<String>, warnings: &mut Vec<Diagnostic>) { | ||
| let message = if scope.is_empty() { Some("include scope must not be empty") } | ||
| else if scope.chars().any(char::is_whitespace) { Some("include scope must not contain whitespace") } | ||
| else if !seen.insert(scope.into()) { Some("include scopes must be unique") } | ||
| else { None }; | ||
| if let Some(message) = message { warnings.push(Diagnostic::warning("include-scope", message)); } | ||
| } | ||
| fn walk(blocks: &[Block], seen: &mut HashSet<String>, warnings: &mut Vec<Diagnostic>) { | ||
| for block in blocks { | ||
| match block { | ||
| Block::Div { attrs, children } => { | ||
| if attrs.classes.iter().any(|c| c == "include") { | ||
| for (_, scope) in attrs.pairs.iter().filter(|(k, _)| k == "scope") { check(scope, seen, warnings); } | ||
| } | ||
| walk(children, seen, warnings); | ||
| } | ||
| Block::BlockQuote { children, .. } => walk(children, seen, warnings), | ||
| Block::List { items, .. } => for item in items { walk(&item.blocks, seen, warnings); }, | ||
| Block::Html { raw, .. } if raw.to_ascii_lowercase().contains("scope") => { | ||
| let dom = parse_fragment(raw, "body"); | ||
| for e in dom.descendants(DOCUMENT) { | ||
| if dom.has_class(e, "include") && let Some(scope) = dom.attr(e, "scope") { check(scope, seen, warnings); } | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
| let mut seen = HashSet::new(); | ||
| walk(&doc.blocks, &mut seen, &mut doc.diagnostics); | ||
| for note in &doc.footnotes { walk(¬e.blocks, &mut seen, &mut doc.diagnostics); } | ||
| } | ||
|
|
||
| pub(crate) fn attrs(open: &str) -> Option<Attr> { | ||
| if !open.to_ascii_lowercase().contains("scope") { return None; } | ||
| let dom = parse_fragment(open, "body"); | ||
| let e = dom.descendants(DOCUMENT).into_iter().find(|&e| dom.has_class(e, "include") && dom.attr(e, "scope").is_some())?; | ||
| let fast5ever::NodeData::Element { attrs, .. } = &dom.get(e).data else { return None; }; | ||
| let mut result = Attr::default(); | ||
| for (name, value) in attrs { | ||
| let key = name.prefix.as_ref().map_or_else(|| name.local.to_string(), |p| format!("{p}:{}", name.local)); | ||
| result.set_pair(key, value); | ||
| } | ||
| Some(result) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.