Skip to content

Commit

Permalink
GH-368: Add support for LaTeX in headings
Browse files Browse the repository at this point in the history
  • Loading branch information
Forsinge committed May 25, 2023
1 parent b4f4b99 commit 4cb40aa
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 26 deletions.
42 changes: 28 additions & 14 deletions packages/latex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ fn transform_verbatim(node: JsonEntry) -> String {
}

fn transform_heading(heading: Value) -> String {
let mut result = String::new();
result.push('[');

let mut vec = vec![];

let Value::String(s) = &heading["arguments"]["level"] else {
panic!();
};
Expand All @@ -226,21 +225,33 @@ fn transform_heading(heading: Value) -> String {
subs.push_str(&"sub".repeat((clamped_level - 1) as usize));
}

write!(
result,
r#"{{"name": "raw", "data": "\n\\{subs}section{{"}},"#,
)
.unwrap();
let heading_style = env::var("heading_style").unwrap_or(String::new());

if heading_style == "unnumbered" {
vec.push(json!(format!("\n\\{subs}section*{{")));
} else {
vec.push(json!(format!("\n\\{subs}section{{")));
}

if let Value::Array(children) = &heading["children"] {
for child in children {
result.push_str(&serde_json::to_string(child).unwrap());
result.push(',');
vec.push(child.clone());
}
}
write!(result, r#"{{"name": "raw", "data": "}}\n"}}"#,).unwrap();
result.push(']');

result
vec.push(json!("}\n"));

if heading_style == "unnumbered" {
vec.push(json!(format!("\\addcontentsline{{toc}}{{{subs}section}}{{")));
if let Value::Array(children) = &heading["children"] {
for child in children {
vec.push(child.clone());
}
}
vec.push(json!("}\n"));
}

serde_json::to_string(&vec).unwrap()
}

fn transform_math(node: Value) -> String {
Expand Down Expand Up @@ -414,7 +425,10 @@ fn manifest() -> String {
"default": "1"
}
],
"type": "parent"
"type": "parent",
"variables": {
"heading_style": {"type": "const", "access": "read"},
},
},

]
Expand Down
11 changes: 2 additions & 9 deletions packages/latex/tests/test_heading.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
],
"__test_transform_to": "latex",
"__test_expected_result": [
{
"data": "\n\\subsection{",
"name": "raw"
},
"\n\\subsection{",
{
"arguments": {},
"data": "Raw stuff",
Expand All @@ -47,10 +44,6 @@
],
"name": "__bold"
},
{
"data": "}\n",
"name": "raw"
}

"}\n"
]
}
43 changes: 40 additions & 3 deletions packages/structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ fn transform_heading(input: Value, to: &str, element: &str) {

print!("{}", serde_json::to_string(&json).unwrap());
}
"latex" => {
let mut json = vec![];
let contents = input["data"].as_str().unwrap();
let level_arg = input["arguments"]["level"].as_str().unwrap();
let level = level_arg.parse::<usize>().unwrap().clamp(1, 3);
let subs = "sub".repeat(level-1);

if element == "unnumbered-heading" {
json.push(json!(format!("\\{subs}section*{{")));
} else {
json.push(json!(format!("\\{subs}section{{")));
}

json.push(inline_content!(contents));
json.push(json!("}\n"));

if element == "unnumbered-heading" {
json.push(json!(format!("\\addcontentsline{{toc}}{{{subs}section}}{{")));
json.push(inline_content!(contents));
json.push(json!("}\n"));
}

print!("{}", serde_json::to_string(&json).unwrap());
}
other => eprintln!("Cannot convert {element} to {other}!"),
}
}
Expand All @@ -111,6 +135,19 @@ fn transform_standalone_heading(input: Value, to: &str) {

print!("{}", serde_json::to_string(&json).unwrap());
}
"latex" => {
let mut json = vec![];
let contents = input["data"].as_str().unwrap();
let level_arg = input["arguments"]["level"].as_str().unwrap();
let level = level_arg.parse::<usize>().unwrap().clamp(1, 3);
let subs = "sub".repeat(level-1);

json.push(json!(format!("\\{subs}section*{{")));
json.push(inline_content!(contents));
json.push(json!("}\n"));

print!("{}", serde_json::to_string(&json).unwrap());
}
other => eprintln!("Cannot convert standalone-heading to {other}!"),
}
}
Expand Down Expand Up @@ -166,7 +203,7 @@ fn manifest() {
},
{
"from": "unnumbered-heading",
"to": ["html"],
"to": ["html", "latex"],
"description": "A heading that does not include a number and is not numbered in a table of contents.",
"type": "inline-module",
"arguments": [
Expand All @@ -182,7 +219,7 @@ fn manifest() {
},
{
"from": "numbered-heading",
"to": ["html"],
"to": ["html", "latex"],
"type": "inline-module",
"description": "A heading that includes number and is numbered in a table of contents.",
"arguments": [
Expand All @@ -198,7 +235,7 @@ fn manifest() {
},
{
"from": "standalone-heading",
"to": ["html"],
"to": ["html", "latex"],
"type": "inline-module",
"description": "A heading is not included in the document's structure or table of contents.",
"arguments": [
Expand Down

0 comments on commit 4cb40aa

Please sign in to comment.