Skip to content

Commit 6013a57

Browse files
author
tjk
committed
Generate documentation
1 parent 4c9b1e3 commit 6013a57

File tree

13 files changed

+1950
-10
lines changed

13 files changed

+1950
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66
resolver = "2"
77

88
[workspace.package]
9-
version = "0.2.0"
9+
version = "0.3.0"
1010
edition = "2021"
1111
authors = ["TJ Kells <[email protected]>"]
1212
license = "MIT"

cli/cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license.workspace = true
77
description = "CLI tool for extended HTML rendering of Markdown with pulldown-cmark"
88

99
[dependencies ]
10-
pulldown-html-ext = { path = "../lib", version = "0.2.0" }
10+
pulldown-html-ext = { path = "../lib", version = "0.3.0" }
1111
toml = "0.8"
1212
clap = { version = "4.4", features = ["derive"] }
1313

lib/docs/examples/basic-usage.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Basic Usage Examples
2+
3+
This guide provides practical examples of common use cases for `pulldown-html-ext`.
4+
5+
## Simple Document Conversion
6+
7+
Convert a basic Markdown document to HTML:
8+
9+
```rust
10+
use pulldown_html_ext::{HtmlConfig, push_html};
11+
12+
fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
let config = HtmlConfig::default();
14+
15+
let markdown = r#"
16+
# Welcome to My Document
17+
18+
This is a paragraph with some **bold** and *italic* text.
19+
20+
## Lists
21+
22+
- Item 1
23+
- Item 2
24+
- Nested item
25+
- Another nested item
26+
- Item 3
27+
28+
1. First ordered item
29+
2. Second ordered item
30+
3. Third ordered item
31+
32+
## Code Example
33+
34+
```rust
35+
fn main() {
36+
println!("Hello, world!");
37+
}
38+
```
39+
"#;
40+
41+
let html = push_html(markdown, &config)?;
42+
println!("{}", html);
43+
Ok(())
44+
}
45+
```
46+
47+
## Working with Files
48+
49+
Read from and write to files:
50+
51+
```rust
52+
use std::fs::File;
53+
use std::io::Read;
54+
use pulldown_html_ext::{HtmlConfig, write_html_io};
55+
56+
fn convert_file(input: &str, output: &str) -> Result<(), Box<dyn std::error::Error>> {
57+
// Read markdown file
58+
let mut markdown = String::new();
59+
File::open(input)?.read_to_string(&mut markdown)?;
60+
61+
// Set up config
62+
let config = HtmlConfig::default();
63+
64+
// Write HTML to file
65+
let output_file = File::create(output)?;
66+
write_html_io(output_file, &markdown, &config)?;
67+
68+
Ok(())
69+
}
70+
71+
fn main() {
72+
if let Err(e) = convert_file("input.md", "output.html") {
73+
eprintln!("Error: {}", e);
74+
}
75+
}
76+
```
77+
78+
## HTML Template Integration
79+
80+
Integrate with an HTML template:
81+
82+
```rust
83+
use pulldown_html_ext::{HtmlConfig, push_html};
84+
85+
fn generate_page(title: &str, content: &str) -> Result<String, Box<dyn std::error::Error>> {
86+
let config = HtmlConfig::default();
87+
let html_content = push_html(content, &config)?;
88+
89+
Ok(format!(
90+
r#"<!DOCTYPE html>
91+
<html lang="en">
92+
<head>
93+
<meta charset="UTF-8">
94+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
95+
<title>{}</title>
96+
<style>
97+
body {{
98+
font-family: system-ui, sans-serif;
99+
line-height: 1.5;
100+
max-width: 800px;
101+
margin: 0 auto;
102+
padding: 2rem;
103+
}}
104+
</style>
105+
</head>
106+
<body>
107+
{}
108+
</body>
109+
</html>"#,
110+
title,
111+
html_content
112+
))
113+
}
114+
115+
fn main() {
116+
let markdown = r#"
117+
# Hello World
118+
119+
This is a test document.
120+
121+
- Item 1
122+
- Item 2
123+
"#;
124+
125+
match generate_page("My Page", markdown) {
126+
Ok(html) => println!("{}", html),
127+
Err(e) => eprintln!("Error: {}", e),
128+
}
129+
}
130+
```
131+
132+
## Multiple Document Processing
133+
134+
Process multiple documents efficiently:
135+
136+
```rust
137+
use pulldown_html_ext::{create_html_renderer, DefaultHtmlWriter};
138+
use pulldown_cmark::Parser;
139+
use pulldown_cmark_escape::FmtWriter;
140+
use std::collections::HashMap;
141+
142+
fn process_documents(
143+
documents: HashMap<String, String>,
144+
) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
145+
let config = HtmlConfig::default();
146+
let mut results = HashMap::new();
147+
148+
for (name, markdown) in documents {
149+
let mut output = String::new();
150+
let writer = DefaultHtmlWriter::new(FmtWriter(&mut output), &config);
151+
let mut renderer = create_html_renderer(writer);
152+
153+
renderer.run(Parser::new(&markdown))?;
154+
results.insert(name, output);
155+
}
156+
157+
Ok(results)
158+
}
159+
160+
fn main() {
161+
let mut docs = HashMap::new();
162+
docs.insert("doc1".to_string(), "# Document 1\nContent...".to_string());
163+
docs.insert("doc2".to_string(), "# Document 2\nContent...".to_string());
164+
165+
match process_documents(docs) {
166+
Ok(results) => {
167+
for (name, html) in results {
168+
println!("Processed {}: {} bytes", name, html.len());
169+
}
170+
}
171+
Err(e) => eprintln!("Error processing documents: {}", e),
172+
}
173+
}
174+
```
175+
176+
## Error Handling
177+
178+
Proper error handling example:
179+
180+
```rust
181+
use pulldown_html_ext::{HtmlConfig, HtmlError, push_html};
182+
use std::error::Error;
183+
184+
fn process_markdown(markdown: &str) -> Result<String, Box<dyn Error>> {
185+
let config = HtmlConfig::default();
186+
187+
match push_html(markdown, &config) {
188+
Ok(html) => Ok(html),
189+
Err(HtmlError::Config(e)) => Err(format!("Configuration error: {}", e).into()),
190+
Err(HtmlError::Render(e)) => Err(format!("Rendering error: {}", e).into()),
191+
Err(HtmlError::Theme(e)) => Err(format!("Theme error: {}", e).into()),
192+
Err(e) => Err(format!("Other error: {}", e).into()),
193+
}
194+
}
195+
196+
fn main() {
197+
let invalid_markdown = "# Title\n\n```invalid-lang\nBad code block\n```";
198+
199+
match process_markdown(invalid_markdown) {
200+
Ok(html) => println!("Success: {}", html),
201+
Err(e) => eprintln!("Error: {}", e),
202+
}
203+
}
204+
```
205+
206+
## Next Steps
207+
208+
- Check out [Custom Configuration Examples](custom-config.md)
209+
- Learn about [Syntax Highlighting Examples](syntax-highlighting.md)
210+
- Explore the [User Guide](../guide/getting-started.md)

0 commit comments

Comments
 (0)