sidoc

Check-in Differences
Login

Check-in Differences

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From sidoc-0.1.1 To trunk

2024-11-21
07:31
Add autoscope, with _if and _opt variants. Leaf check-in: 953a47b21e user: jan tags: trunk, sidoc-0.1.2
2024-11-07
09:13
change log. check-in: ef4f5138fd user: jan tags: trunk
08:53
Update crate metadata. Happy pedantic clippy. check-in: c84ce1ad85 user: jan tags: trunk, sidoc-0.1.1
2024-11-06
18:42
Happy pedantic clippy. check-in: c1d63406c5 user: jan tags: trunk

Changes to Cargo.toml.

1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







[package]
name = "sidoc"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "0BSD"
# https://crates.io/category_slugs
categories = [ "template-engine" ]
keywords = [ "text", "document", "html" ]
repository = "https://repos.qrnch.tech/pub/sidoc"
description = "Generate structured/scoped indented documents."

Changes to src/builder.rs.

29
30
31
32
33
34
35























































































36
37
38
39
40
41
42
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    if let Some(ln) = term_line {
      self.scope_stack.push(Some(ln.to_string()));
    } else {
      self.scope_stack.push(None);
    }
    self
  }

  /// Wrap [`Builder::scope()`] and [`Builder::exit()`].
  ///
  /// Initialize a new scope, call caller-supplied closure, and automatically
  /// exit scope before returning.
  ///
  /// ```
  /// use std::sync::Arc;
  /// use sidoc::{Builder, RenderContext};
  ///
  /// let mut bldr = Builder::new();
  ///
  /// bldr
  ///   .line("<!DOCTYPE html>")
  ///   .autoscope("<html>", Some("</html>"), |bldr| {
  ///     bldr.autoscope("<head>", Some("</head>"), |bldr| {
  ///       bldr.line("<title>hello</title>");
  ///     });
  ///   });
  ///
  /// let doc = bldr.build().unwrap();
  /// let mut r = RenderContext::new();
  /// r.doc("root", Arc::new(doc));
  /// let buf = r.render("root").unwrap();
  ///
  /// assert_eq!(
  ///   buf,
  ///   "<!DOCTYPE html>\n<html>\n  <head>\n    <title>hello</title>\n  </head>\n</html>\n"
  /// );
  /// ```
  #[allow(clippy::needless_pass_by_value)]
  pub fn autoscope<F, L: ToString, K: ToString>(
    &mut self,
    begin_line: L,
    term_line: Option<K>,
    f: F
  ) -> &mut Self
  where
    F: FnOnce(&mut Self)
  {
    self.scope(begin_line, term_line);
    f(self);
    self.exit();
    self
  }

  /// Same as [`Builder::autoscope()`], but only init scope and call closure if
  /// predicate is true.
  #[allow(clippy::needless_pass_by_value)]
  pub fn autoscope_if<F, L: ToString, K: ToString>(
    &mut self,
    pred: bool,
    begin_line: L,
    term_line: Option<K>,
    f: F
  ) -> &mut Self
  where
    F: FnOnce(&mut Self)
  {
    if pred {
      self.scope(begin_line, term_line);
      f(self);
      self.exit();
    }
    self
  }

  /// Same as [`Builder::autoscope()`], but only init scope and call closure if
  /// `opt` is `Some(T)`.  `T` will be passed to the closure.
  #[allow(clippy::needless_pass_by_value)]
  pub fn autoscope_opt<F, T, L: ToString, K: ToString>(
    &mut self,
    opt: Option<T>,
    begin_line: L,
    term_line: Option<K>,
    f: F
  ) -> &mut Self
  where
    F: FnOnce(&mut Self, T)
  {
    if let Some(o) = opt {
      self.scope(begin_line, term_line);
      f(self, o);
      self.exit();
    }
    self
  }

  /// Leave a previously entered scope.
  ///
  /// If the `scope()` call that created the current scope
  ///
  /// # Panics
  /// The scope stack must not be empty.

Added tests/simple_html.rs.














































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
use std::sync::Arc;

use sidoc::{Builder, RenderContext};

#[test]
fn simple_html() {
  let mut bldr = Builder::new();

  bldr.line("<!DOCTYPE html>");
  bldr.scope("<html>", Some("</html>")).exit();

  let doc = bldr.build().unwrap();

  let mut r = RenderContext::new();

  r.doc("hello", Arc::new(doc));

  let buf = r.render("hello").unwrap();

  assert_eq!(buf, "<!DOCTYPE html>\n<html>\n</html>\n");
}


#[test]
fn simple_html_head() {
  let mut bldr = Builder::new();

  bldr
    .line("<!DOCTYPE html>")
    .scope("<html>", Some("</html>"))
    .scope("<head>", Some("</head>"))
    .exit()
    .exit();

  let doc = bldr.build().unwrap();

  let mut r = RenderContext::new();

  r.doc("root", Arc::new(doc));

  let buf = r.render("root").unwrap();

  assert_eq!(
    buf,
    "<!DOCTYPE html>\n<html>\n  <head>\n  </head>\n</html>\n"
  );
}


#[test]
fn autoscope() {
  let mut bldr = Builder::new();

  bldr
    .line("<!DOCTYPE html>")
    .autoscope("<html>", Some("</html>"), |bldr| {
      bldr.autoscope("<head>", Some("</head>"), |bldr| {
        bldr.line("<title>hello</title>");
      });
    });

  let doc = bldr.build().unwrap();

  let mut r = RenderContext::new();

  r.doc("root", Arc::new(doc));

  let buf = r.render("root").unwrap();

  assert_eq!(
    buf,
    "<!DOCTYPE html>\n<html>\n  <head>\n    <title>hello</title>\n  \
     </head>\n</html>\n"
  );
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :

Deleted tests/simple_http.rs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

















































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
use std::sync::Arc;

use sidoc::{Builder, RenderContext};

#[test]
fn simple_html() {
  let mut bldr = Builder::new();

  bldr.line("<!DOCTYPE html>");
  bldr.scope("<html>", Some("</html>")).exit();

  let doc = bldr.build().unwrap();

  let mut r = RenderContext::new();

  r.doc("hello", Arc::new(doc));

  let buf = r.render("hello").unwrap();

  assert_eq!(buf, "<!DOCTYPE html>\n<html>\n</html>\n");
}


#[test]
fn simple_html_head() {
  let mut bldr = Builder::new();

  bldr
    .line("<!DOCTYPE html>")
    .scope("<html>", Some("</html>"))
    .scope("<head>", Some("</head>"))
    .exit()
    .exit();

  let doc = bldr.build().unwrap();

  let mut r = RenderContext::new();

  r.doc("root", Arc::new(doc));

  let buf = r.render("root").unwrap();

  assert_eq!(
    buf,
    "<!DOCTYPE html>\n<html>\n  <head>\n  </head>\n</html>\n"
  );
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :

Changes to www/changelog.md.

1
2
3
4
5
6
7

8
9
10
11







12
13
14
15
16










17
18
19
20
21
22
1
2
3
4
5
6

7
8
9
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38






-
+



-
+
+
+
+
+
+
+





+
+
+
+
+
+
+
+
+
+






# Change Log

⚠️  indicates a breaking change.

## [Unreleased]

[Details](/vdiff?from=sidoc-0.1.0&to=trunk)
[Details](/vdiff?from=sidoc-0.1.1&to=trunk)

### Added

- Derive `Default` on `Doc` and `RenderContext`
- `Builder::autoscope()` is a helper for adding scopes that will automatically
  be closed.
- `Builder::autoscope_if()` is a variant of `Builder::autoscope()` that will
  only add a scope, and call callback, if a predicate is true.
- `Builder::autoscope_opt()` is another variant of `Builder::autoscope()` that
  will only add scope, and call callback, if an input option paramter is
  `Some(T)`.

### Changed

### Removed

---

## [0.1.1] - 2024-11-07

[Details](/vdiff?from=sidoc-0.1.0&to=sidoc-0.1.1)

### Added

- Derive `Default` on `Doc` and `RenderContext`

---

## [0.1.0] - 2020-09-12

Initial release