#text #document #html

sidoc

Library for generating structured/scoped indented documents

1 unstable release

0.1.0 Sep 12, 2020

#12 in #document

23 downloads per month
Used in sidoc-html5

0BSD license

10KB
200 lines


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