arborium_docsrs_demo/lib.rs
1//! # arborium docs.rs demo
2//!
3//! This crate demonstrates [arborium](https://github.com/bearcove/arborium)
4//! syntax highlighting on docs.rs. Rust code is already highlighted by rustdoc,
5//! but other languages are left plain. arborium fixes that!
6//!
7//! ## TOML
8//!
9//! Configuration files are everywhere in the Rust ecosystem:
10//!
11//! ```toml
12//! [package]
13//! name = "my-awesome-crate"
14//! version = "1.0.0"
15//! edition = "2021"
16//!
17//! [dependencies]
18//! serde = { version = "1.0", features = ["derive"] }
19//! tokio = { version = "1", features = ["full"] }
20//!
21//! [dev-dependencies]
22//! criterion = "0.5"
23//!
24//! [features]
25//! default = ["std"]
26//! std = []
27//! async = ["tokio"]
28//! ```
29//!
30//! ## Shell
31//!
32//! Installation and usage instructions often include shell commands:
33//!
34//! ```bash
35//! # Install the crate
36//! cargo install my-awesome-crate
37//!
38//! # Run with arguments
39//! my-awesome-crate --config config.toml --verbose
40//!
41//! # Set environment variables
42//! export RUST_LOG=debug
43//! cargo run -- serve --port 8080
44//! ```
45//!
46//! ## JSON
47//!
48//! API responses, configuration, and data interchange:
49//!
50//! ```json
51//! {
52//! "name": "arborium",
53//! "version": "1.0.0",
54//! "features": ["syntax-highlighting", "tree-sitter", "wasm"],
55//! "languages": 69,
56//! "config": {
57//! "theme": "tokyo-night",
58//! "maxDepth": 3
59//! }
60//! }
61//! ```
62//!
63//! ## YAML
64//!
65//! CI/CD pipelines and Kubernetes configs:
66//!
67//! ```yaml
68//! name: CI
69//! on: [push, pull_request]
70//!
71//! jobs:
72//! test:
73//! runs-on: ubuntu-latest
74//! steps:
75//! - uses: actions/checkout@v4
76//! - name: Install Rust
77//! uses: dtolnay/rust-toolchain@stable
78//! - name: Run tests
79//! run: cargo test --all-features
80//! ```
81//!
82//! ## SQL
83//!
84//! Database queries and migrations:
85//!
86//! ```sql
87//! -- Create a users table
88//! CREATE TABLE users (
89//! id SERIAL PRIMARY KEY,
90//! username VARCHAR(255) NOT NULL UNIQUE,
91//! email VARCHAR(255) NOT NULL,
92//! created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
93//! );
94//!
95//! -- Query with joins
96//! SELECT u.username, COUNT(p.id) as post_count
97//! FROM users u
98//! LEFT JOIN posts p ON p.author_id = u.id
99//! WHERE u.created_at > '2024-01-01'
100//! GROUP BY u.id
101//! ORDER BY post_count DESC
102//! LIMIT 10;
103//! ```
104//!
105//! ## JavaScript
106//!
107//! Browser code and Node.js examples:
108//!
109//! ```javascript
110//! import { loadGrammar, highlight } from '@arborium/arborium';
111//!
112//! async function highlightCode(language, source) {
113//! const grammar = await loadGrammar(language);
114//! const html = grammar.highlight(source);
115//! document.getElementById('output').innerHTML = html;
116//! }
117//!
118//! // Auto-highlight all code blocks
119//! document.querySelectorAll('pre code').forEach(block => {
120//! const lang = block.className.match(/language-(\w+)/)?.[1];
121//! if (lang) highlightCode(lang, block.textContent);
122//! });
123//! ```
124//!
125//! ## TypeScript
126//!
127//! Type-safe JavaScript:
128//!
129//! ```typescript
130//! interface HighlightConfig {
131//! theme: string;
132//! selector: string;
133//! maxDepth?: number;
134//! }
135//!
136//! async function highlight(
137//! language: string,
138//! source: string,
139//! config?: HighlightConfig
140//! ): Promise<string> {
141//! const grammar = await loadGrammar(language);
142//! return grammar.highlight(source);
143//! }
144//!
145//! const config: HighlightConfig = {
146//! theme: 'mocha',
147//! selector: 'pre > code',
148//! };
149//! ```
150//!
151//! ## Python
152//!
153//! Scripts and tooling:
154//!
155//! ```python
156//! import subprocess
157//! from pathlib import Path
158//!
159//! def build_and_test(project_dir: Path) -> bool:
160//! """Build the project and run tests."""
161//! result = subprocess.run(
162//! ["cargo", "test", "--all-features"],
163//! cwd=project_dir,
164//! capture_output=True,
165//! text=True,
166//! )
167//! if result.returncode != 0:
168//! print(f"Tests failed:\n{result.stderr}")
169//! return False
170//! return True
171//!
172//! if __name__ == "__main__":
173//! success = build_and_test(Path.cwd())
174//! exit(0 if success else 1)
175//! ```
176//!
177//! ## Dockerfile
178//!
179//! Container builds:
180//!
181//! ```dockerfile
182//! FROM rust:1.75 as builder
183//! WORKDIR /app
184//! COPY Cargo.toml Cargo.lock ./
185//! COPY src ./src
186//! RUN cargo build --release
187//!
188//! FROM debian:bookworm-slim
189//! RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
190//! COPY --from=builder /app/target/release/myapp /usr/local/bin/
191//! ENTRYPOINT ["myapp"]
192//! ```
193//!
194//! ## Nix
195//!
196//! Reproducible builds:
197//!
198//! ```nix
199//! { pkgs ? import <nixpkgs> {} }:
200//!
201//! pkgs.rustPlatform.buildRustPackage rec {
202//! pname = "arborium";
203//! version = "1.0.0";
204//!
205//! src = ./.;
206//!
207//! cargoLock = {
208//! lockFile = ./Cargo.lock;
209//! };
210//!
211//! nativeBuildInputs = [ pkgs.pkg-config ];
212//! buildInputs = [ pkgs.openssl ];
213//! }
214//! ```
215//!
216//! ## GraphQL
217//!
218//! API schemas:
219//!
220//! ```graphql
221//! type Query {
222//! user(id: ID!): User
223//! posts(limit: Int = 10, offset: Int = 0): [Post!]!
224//! }
225//!
226//! type User {
227//! id: ID!
228//! username: String!
229//! email: String!
230//! posts: [Post!]!
231//! }
232//!
233//! type Post {
234//! id: ID!
235//! title: String!
236//! content: String!
237//! author: User!
238//! createdAt: DateTime!
239//! }
240//!
241//! mutation CreatePost($input: CreatePostInput!) {
242//! createPost(input: $input) {
243//! id
244//! title
245//! }
246//! }
247//! ```
248//!
249//! ## CSS
250//!
251//! Styling:
252//!
253//! ```css
254//! :root {
255//! --primary-color: #50C878;
256//! --background: #1a1b26;
257//! --foreground: #c0caf5;
258//! }
259//!
260//! .code-block {
261//! background: var(--background);
262//! color: var(--foreground);
263//! padding: 1rem;
264//! border-radius: 8px;
265//! font-family: 'Iosevka', monospace;
266//! overflow-x: auto;
267//! }
268//!
269//! .code-block a-k { color: #bb9af7; } /* keywords */
270//! .code-block a-s { color: #9ece6a; } /* strings */
271//! .code-block a-n { color: #ff9e64; } /* numbers */
272//! ```
273//!
274//! ## Rust (for comparison)
275//!
276//! Rust code is already highlighted by rustdoc - arborium skips it:
277//!
278//! ```rust
279//! use std::collections::HashMap;
280//!
281//! pub fn highlight(language: &str, source: &str) -> String {
282//! let mut cache: HashMap<String, Grammar> = HashMap::new();
283//!
284//! let grammar = cache
285//! .entry(language.to_string())
286//! .or_insert_with(|| load_grammar(language));
287//!
288//! grammar.highlight(source)
289//! }
290//! ```
291//!
292//! ---
293//!
294//! This demo is powered by [arborium](https://github.com/bearcove/arborium).
295//! See the [integration guide](https://arborium.bearcove.eu/) for setup instructions.
296
297// Empty crate - this is just a docs demo