#document #text-document #text #html

sidoc

Generate structured/scoped indented documents

3 releases

0.1.2 Nov 21, 2024
0.1.1 Nov 7, 2024
0.1.0 Sep 12, 2020

#142 in Template engine

Download history 5/week @ 2024-08-26 18/week @ 2024-09-23 24/week @ 2024-09-30 5/week @ 2024-10-07 122/week @ 2024-11-04 13/week @ 2024-11-11 152/week @ 2024-11-18 12/week @ 2024-11-25 32/week @ 2024-12-02 28/week @ 2024-12-09

226 downloads per month
Used in 4 crates (2 directly)

0BSD license

14KB
261 lines

sidoc

Template engine designed to generate indented documents.


lib.rs:

A library for generating indented scoped text documents consisting of reusable parts.

There are three major steps involved in using idoc:

  • Creating document(s)
  • Adding document(s) to a render context
  • Render output

Simple example

use std::sync::Arc;
use sidoc::{Builder, RenderContext};

fn create_doc() {
  // Use a builder to generate document
  let mut bldr = Builder::new();
  bldr.line("<!DOCTYPE html>");
  bldr.scope("<html>", Some("</html>")).exit();
  let doc = bldr.build().unwrap();

  // Create a render context, add document to it
  let mut r = RenderContext::new();
  r.doc("root", Arc::new(doc));

  // Render the output
  let buf = r.render("root").unwrap();
  assert_eq!(buf, "<!DOCTYPE html>\n<html>\n</html>\n");
}

Reference example

use std::sync::Arc;
use sidoc::{Builder, RenderContext};

fn create_doc() {
  // Use a builder to generate root document
  let mut bldr = Builder::new();
  bldr.scope("<html>", Some("</html>")).exit();
  let doc = bldr.build().unwrap();

  // Use a builder to generate sub document
  let mut bldr = Builder::new();
  bldr.scope("<head>", Some("</head>")).exit();
  let subdoc = bldr.build().unwrap();

  // Create a render context, add documents to it
  let mut r = RenderContext::new();
  r.doc("root", Arc::new(doc));
  r.doc("head", Arc::new(subdoc));

  // Render the output
  let buf = r.render("root").unwrap();
  assert_eq!(buf, "<html>\n  <head>\n  </head>\n</html>\n");
}

No runtime deps