Skip to content
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

Add djot support, make a test_djot_book folder. #2483

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ guide/book
.vscode
tests/dummy_book/book/
test_book/book/
test_djot_book/book/

# Ignore Jetbrains specific files.
.idea/
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ clap_complete = "4.3.2"
once_cell = "1.17.1"
env_logger = "0.11.1"
handlebars = "6.0"
jotdown = "0.6"
log = "0.4.17"
memchr = "2.5.0"
opener = "0.7.0"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
[![crates.io](https://img.shields.io/crates/v/mdbook.svg)](https://crates.io/crates/mdbook)
[![LICENSE](https://img.shields.io/github/license/rust-lang/mdBook.svg)](LICENSE)

mdBook is a utility to create modern online books from Markdown files.
mdBook is a utility to create modern online books from Markdown or Djot files.

Check out the **[User Guide]** for a list of features and installation and usage information.
The User Guide also serves as a demonstration to showcase what a book looks like.

If a file ends in .dj it will be treated as a djot file. Check out the test_djot_book for djot
specific markup.

If you are interested in contributing to the development of mdBook, check out the [Contribution Guide].

## License
Expand Down
14 changes: 12 additions & 2 deletions src/preprocess/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use regex::Regex;
use std::ffi::OsStr;
use std::path::Path;

use super::{Preprocessor, PreprocessorContext};
Expand Down Expand Up @@ -37,7 +38,16 @@ impl Preprocessor for IndexPreprocessor {
warn_readme_name_conflict(&path, &&mut index_md);
}

path.set_file_name("index.md");
let mut index_dj = source_dir.join(path.with_file_name("index.dj"));
if index_dj.exists() {
warn_readme_name_conflict(&path, &&mut index_dj);
}

if Some(OsStr::new("dj")) == path.extension() {
path.set_file_name("index.dj");
} else {
path.set_file_name("index.md");
}
}
}
}
Expand All @@ -54,7 +64,7 @@ fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
.parent()
.unwrap_or_else(|| index_path.as_ref());
warn!(
"It seems that there are both {:?} and index.md under \"{}\".",
"It seems that there are both {:?} and index.md or index.dj under \"{}\".",
file_name,
parent_dir.display()
);
Expand Down
28 changes: 21 additions & 7 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::utils;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -54,13 +55,22 @@ impl HtmlHandlebars {
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
}

let content = utils::render_markdown(&ch.content, ctx.html_config.smart_punctuation());
let content = if Some(OsStr::new("dj")) == path.extension() {
utils::render_djot(&ch.content)?
} else {
utils::render_markdown(&ch.content, ctx.html_config.smart_punctuation())
};

let fixed_content = if Some(OsStr::new("dj")) == path.extension() {
utils::render_djot(&ch.content)?
} else {
utils::render_markdown_with_path(
&ch.content,
ctx.html_config.smart_punctuation(),
Some(path),
)
};

let fixed_content = utils::render_markdown_with_path(
&ch.content,
ctx.html_config.smart_punctuation(),
Some(path),
);
if !ctx.is_index && ctx.html_config.print.page_break {
// Add page break between chapters
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before
Expand Down Expand Up @@ -124,7 +134,11 @@ impl HtmlHandlebars {
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;

if ctx.is_index {
ctx.data.insert("path".to_owned(), json!("index.md"));
if Some(OsStr::new("dj")) == path.extension() {
ctx.data.insert("path".to_owned(), json!("index.dj"));
} else {
ctx.data.insert("path".to_owned(), json!("index.md"));
}
ctx.data.insert("path_to_root".to_owned(), json!(""));
ctx.data.insert("is_index".to_owned(), json!(true));
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod fs;
mod string;
pub(crate) mod toml_ext;
use crate::errors::Error;
use jotdown::{html::Indentation, Render};
use log::error;
use once_cell::sync::Lazy;
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd};
Expand Down Expand Up @@ -189,6 +190,19 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
}
}

/// Wrapper around the jotdown parser for rendering djot to HTML.
pub fn render_djot(text: &str) -> anyhow::Result<String> {
let events = jotdown::Parser::new(text);
let mut content = String::new();
let renderer = jotdown::html::Renderer::indented(Indentation {
string: " ".repeat(4),
initial_level: 6,
});
renderer.push(events, &mut content)?;
let content_stripped = content.trim().to_string();
Ok(content_stripped)
}

/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
pub fn render_markdown(text: &str, smart_punctuation: bool) -> String {
render_markdown_with_path(text, smart_punctuation, None)
Expand Down
27 changes: 27 additions & 0 deletions test_djot_book/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[book]
title = "mdBook test book"
description = "A demo book to test and validate changes"
authors = ["YJDoc2"]
language = "en"

[rust]
edition = "2018"

[output.html]
mathjax-support = true

[output.html.playground]
editable = true
line-numbers = true

[output.html.search]
limit-results = 20
use-boolean-and = true
boost-title = 2
boost-hierarchy = 2
boost-paragraph = 1
expand = true
heading-split-level = 2

[output.html.redirect]
"/format/config.html" = "configuration/index.html"
24 changes: 24 additions & 0 deletions test_djot_book/src/README.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Demo Book

This is a simple demo book, which is intended to be used for verifying and validating style changes in mdBook.
This contains dummy examples of various djot elements and code languages, so that one can check changes made in mdBook styles.

This rough outline is :

: individual

contains basic djot elements such as headings, paragraphs, links etc.

: languages

contains a `hello world` in each of supported language to see changes in syntax highlighting

: rust

contains language examples specific to rust, such as play pen, runnable examples etc.

: djot specific inline

contains djot specific items that are inline.

This is more for checking and fixing style, rather than verifying that correct code is generated for given djot, that is better handled in tests.
50 changes: 50 additions & 0 deletions test_djot_book/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Summary

[Prefix Chapter](prefix.dj)

---

- [Introduction](README.dj)
- [Draft Chapter]()

# Actual Djot Tag Examples

- [Djot Individual tags](individual/README.dj)
- [Heading](individual/heading.dj)
- [Paragraphs](individual/paragraph.dj)
- [Line Break](individual/linebreak.dj)
- [Emphasis](individual/emphasis.dj)
- [Blockquote](individual/blockquote.dj)
- [List](individual/list.dj)
- [Code](individual/code.dj)
- [Image](individual/image.dj)
- [Links and Horizontal Rule](individual/link_hr.dj)
- [Tables](individual/table.dj)
- [Tasks](individual/task.dj)
- [Strikethrough](individual/strikethrough.dj)
- [MathJax](individual/mathjax.dj)
- [Mixed](individual/mixed.dj)
- [Thematic Break](individual/thematic_break.dj)
- [Languages](languages/README.dj)
- [Syntax Highlight](languages/highlight.dj)
- [Rust Specific](rust/README.dj)
- [Rust Codeblocks](rust/rust_codeblock.dj)
- [Djot Specific Inline](djot_specific_inline/README.dj)
- [Comment](djot_specific_inline/comment.dj)
- [Footnote](djot_specific_inline/footnote.dj)
- [Highlight](djot_specific_inline/highlight.dj)
- [Inline Attributes](djot_specific_inline/inline_attributes.dj)
- [Insert](djot_specific_inline/insert.dj)
- [Raw Inline](djot_specific_inline/raw_inline.dj)
- [Span](djot_specific_inline/span.dj)
- [Subscript](djot_specific_inline/subscript.dj)
- [Superscript](djot_specific_inline/superscript.dj)
- [Symbols](djot_specific_inline/symbols.dj)
- [Djot Specific Block](djot_specific_block/README.dj)
- [Div](djot_specific_block/div.dj)
- [Raw Block](djot_specific_block/raw_block.dj)
- [Footnote](djot_specific_block/footnote.dj)

---

[Suffix Chapter](suffix.dj)
1 change: 1 addition & 0 deletions test_djot_book/src/djot_specific_block/README.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Djot Specific Block
7 changes: 7 additions & 0 deletions test_djot_book/src/djot_specific_block/div.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Div

::: warning
Here is a paragraph.

And here is another.
:::
5 changes: 5 additions & 0 deletions test_djot_book/src/djot_specific_block/footnote.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Footnote

Here's the reference.[^one]

[^one]: This is a footnote.
9 changes: 9 additions & 0 deletions test_djot_book/src/djot_specific_block/raw_block.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Raw Block

``` =html
<p> a html paragraph</p>
```

``` =plaintext
a plaintext paragraph
```
1 change: 1 addition & 0 deletions test_djot_book/src/djot_specific_inline/README.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Djot Specific Inline
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/comment.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Comment

Here is a {% comment %}.
5 changes: 5 additions & 0 deletions test_djot_book/src/djot_specific_inline/footnote.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Footnote

Here is a reference.[^ref]

[^ref]: Here is a footnote.
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/highlight.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Highlight

This is {=highlighted text=}.
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/inline_attributes.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Inline Attributes

Inline attributes{.class #id key="value"}
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/insert.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Insert

{+insert+}
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/raw_inline.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Raw Inline

This is `<?php echo 'Raw Inline' ?>`{=html}.
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/span.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Span

This is a [span]{.wonderful}.
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/subscript.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Subscript

sub~script~
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/superscript.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Superscript

super^script^
3 changes: 3 additions & 0 deletions test_djot_book/src/djot_specific_inline/symbols.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Symbols

Support for symbols is optional :+1: :smiley:!
17 changes: 17 additions & 0 deletions test_djot_book/src/individual/README.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Djot Individual tags

This contains following tags:

- Headings
- Paragraphs
- Line breaks
- Emphasis
- Blockquotes
- Lists
- Code blocks
- Images
- Links and Horizontal rules
- Tables
- Task Lists (not supported)
- Strikethrough
- Mixed
30 changes: 30 additions & 0 deletions test_djot_book/src/individual/blockquote.dj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Blockquote

> This is a quoted sentence.

> This is a quoted paragraph
>
> separated lines
> here

> Nested
>
> > Quoted
> > Paragraph

> ### And now,
>
> *Let us _introduce_*
> All kinds of
>
> - tags
> - etc
> - stuff
>
> 1. In
> 2. The
> 3. blockquote
>
> > cause we can
> >
> > > Cause we can
Loading