Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sitandr committed Nov 3, 2023
1 parent acd53c5 commit 2cef230
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Rendered looks like this:

![Example](img/image_2.png)

It comes with prelude that sets `width: 300pt`, `margin: 0.5cm` and `height: auto`. To disable it, add `typ-noprelude` as codeblock language.
It comes with prelude that sets `width: 300pt`, `margin: 0.5cm` and `height: auto`. To disable it, add `typ-nopreamble` as codeblock language.

You can also disable certain blocks (but still highlight them) using `typ-norender`.

Expand Down
2 changes: 1 addition & 1 deletion example-book/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ title = "Example book about Typst"

[preprocessor.typst-highlight]
#disable_inline = true
#typst_default = true
typst_default = true
render = true
10 changes: 9 additions & 1 deletion example-book/src/chapter_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
This is a test
```

# Render&Preamble

```typ-norender
This will not be rendered.
And that way?
```

```typ-nopreambule
```typ-nopreamble
This will be default doc
```

This is some `#inline` code.

```
This is code without any lang specified.
```
41 changes: 21 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use syntect::html::{
use syntect::parsing::SyntaxSetBuilder;
use syntect::util::LinesWithEndings;

static PREAMBULE: &str = "
#set page(height: auto, width: 200pt, margin: 0.5cm)
static PREAMBLE: &str = "
#set page(height: auto, width: 400pt, margin: 0.5cm)
";

lazy_static! {
Expand Down Expand Up @@ -166,7 +166,7 @@ fn process_chapter(
text,
chapter_path.clone(),
chapter.name.clone(),
!lang.contains("nopreambule"),
!lang.contains("nopreamble"),
);

compile_errors.extend(err);
Expand All @@ -187,6 +187,7 @@ fn process_chapter(
format!(r#"<div style="margin-bottom: 0.5em">{}</div>"#, html)
.into(),
));
new_events.push(Event::HardBreak);
codeblock_text = None
} else {
new_events.push(Event::End(tag))
Expand Down Expand Up @@ -225,24 +226,23 @@ fn process_chapter(
}

fn get_lang<'a>(t: &'a Tag, typst_default: bool) -> Option<&'a str> {
let default = if typst_default {
Some("typ")
} else {
None
};
if let Tag::CodeBlock(ref kind) = *t {
match kind {
CodeBlockKind::Fenced(kind) => Some(kind.as_ref()),
CodeBlockKind::Indented => {
if typst_default {
Some("typ")
} else {
None
}
}
CodeBlockKind::Fenced(kind) => (!kind.is_empty()).then(|| kind.as_ref()).or(default),
CodeBlockKind::Indented => default
}
} else {
None
}
}

fn is_typst_codeblock(s: &str) -> bool {
s.contains("typ") || s.contains("typ")
s.contains("typ") || s.contains("typst")
}

fn highlight(s: CowStr, inline: bool) -> Result<String> {
Expand Down Expand Up @@ -293,7 +293,7 @@ fn render_block(
src: String,
mut dir: PathBuf,
name: String,
preambule: bool,
preamble: bool,
) -> (String, Option<impl Future<Output = ()>>) {
let filename = sha256_hash(&src);
let mut output = dir.clone();
Expand All @@ -309,26 +309,27 @@ fn render_block(
dir.push(filename.clone() + ".typ");

let mut file = File::create(&dir).expect("Can't create file");
if preambule {
writeln!(file, "{}", PREAMBULE).expect("Error writing to file")
if preamble {
writeln!(file, "{}", PREAMBLE).expect("Error writing to file")
};
write!(file, "{}", src).expect("Error writing to file");

let res = Command::new("typst")
.arg("c")
.arg(dir)
.arg(&dir)
.arg("--root")
.arg(dir.parent().unwrap().parent().unwrap())
.arg(&output)
.output();

command = Some(async move {
let output = res.await.expect("Failed").stderr;
let output = String::from_utf8_lossy(&output);

if !output.trim().is_empty() {
if !output.is_empty() {
let stderr = std::io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "Error at \"{}\"\n", name).expect("Can't write to stderr");
writeln!(handle, "{}", output).expect("Can't write to stderr");
writeln!(handle, "Error at chapter \"{}\"\n", name).expect("Can't write to stderr");
handle.write_all(&output).expect("Can't write to stderr");
}
});
}
Expand Down

0 comments on commit 2cef230

Please sign in to comment.