Crates.io | markdown_to_html_parser |
lib.rs | markdown_to_html_parser |
version | 0.1.0 |
source | src |
created_at | 2023-11-13 18:27:50.31572 |
updated_at | 2023-11-13 18:27:50.31572 |
description | The library parses Markdown syntax into HTML |
homepage | |
repository | |
max_upload_size | |
id | 1033898 |
size | 23,384 |
This is a simple Markdown to HTML parser implemented in Rust using the pest parser generator library. It can convert Markdown text into HTML.
To use this Markdown to HTML parser in your Rust project, add it as a dependency in your Cargo.toml file:
[dependencies]
markdown_to_html_parser = "0.1.0"
First, import the library in your Rust code:
use markdown_parser::parse_markdown;
Then, you can convert Markdown text to HTML using the parse_markdown function:
let markdown_text = "Your Markdown text here";
let html = parse_markdown(markdown_text);
println!("{}", html);
You can also use this Markdown to HTML parser as a command-line tool. To do this, first install the Rust toolchain on your system. Then, clone the repository and build the project:
$ make help
$ make run -f <file_name> -o <output_file_name> --is_force true
Here's an example of how to use the Markdown to HTML parser in a Rust program:
use markdown_to_html_parser::parse_markdown;
fn main() {
let markdown_text = "# Heading 1\nSome *italic* and **bold** text.\n## Heading 2\nMore text.";
let html = parse_markdown(markdown_text);
println!("{}", html);
}
Running this program will produce the following HTML output:
<h1>Heading 1</h1><p>Some <em>italic</em> and <strong>bold</strong> text.</p><h2>Heading 2</h2><p>More text.</p>
This parser uses a simple grammar to recognize Markdown elements. The grammar is defined in the markdown.pest
file, and it includes rules for headers and paragraphs. Here's a brief overview of the grammar:
markdown
It consists of a sequence of elements, element_inner, text, and new_line, repeated zero or more times.elements
It represents a choice between different types of elements: h1, h2, italic_text, bold_text, and code_text.h1
Starts with "# " and is followed by a sequence of elements, element_inner, or text, ending with "\n".h2
Similar to h1 but starts with "## ".italic_text
Represents text enclosed within either "_" or "*", denoting italic formatting.bold_text
Represents text enclosed within either "__" or "**", denoting bold formatting.code_text
Represents text enclosed within "`", denoting code formatting.element_inner
Represents a sequence of characters that are not "_", "*", "`", or "\n".text
Represents a sequence of characters that are not "\n".new_line
Represents a newline character.Contributions are welcome! If you want to improve the parser, fix bugs, or add new features, please open an issue or submit a pull request on the GitHub repository.
[TODO] This Markdown to HTML parser is open-source and licensed under the MIT License. See the LICENSE file for more details.