1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.templates.part;
7 
8 import diamond.templates.contentmode;
9 import diamond.templates.grammar;
10 
11 /**
12 * Wrapper for a template part.
13 * Template parts are the aprsed parts of the view.
14 */
15 final class Part
16 {
17   package(diamond.templates):
18   /// The name.
19   immutable(string) _name;
20   /// The current grammar.
21   Grammar _currentGrammar;
22   /// The seek index.
23   size_t _seekIndex;
24   /// The content.
25   string _content;
26 
27   final:
28   /// Creates a new template part.
29   this()
30   {
31     _name = "html";
32   }
33 
34   /// Increases the seek index.
35   void increaseSeekIndex() pure nothrow
36   {
37     _seekIndex++;
38   }
39 
40   /// Decreases the seek index.
41   void decreaseSeekIndex() pure nothrow {
42     _seekIndex--;
43   }
44 
45   /**
46   * Checks whether it's the start of the part or not.
47   * Returns:
48   *   True if it's start of the part, false otherwise.
49   */
50   auto isStart()
51   {
52     return _seekIndex == 1;
53   }
54 
55   /**
56   * Checks whether the current character will end the scope of this part.
57   * Params:
58   *   currentChar = The current char.
59   * Returns:
60   *   True if the character marks the end of the part, false otherwise.
61   */
62   auto isEnd(char currentChar) nothrow
63   {
64     return (_currentGrammar !is null &&
65       currentChar == _currentGrammar.endCharacter && _seekIndex < 1);
66   }
67 
68   @property
69   {
70     /// Sets the current grammar.
71     void currentGrammar(Grammar newGrammar)
72     {
73       _currentGrammar = newGrammar;
74     }
75   }
76 
77   public:
78   @property
79   {
80     /// Gets the current grammar.
81     Grammar currentGrammar() { return _currentGrammar; }
82 
83     /// Gets the name of the part.
84     const(string) name() { return _currentGrammar ? _currentGrammar.name : _name; }
85 
86     /// Gets the content of the part.
87     const(string) content() { return _content; }
88 
89     /// Gets the content mode.
90     const(ContentMode) contentMode()
91     {
92       return !_currentGrammar ?
93         ContentMode.appendContent : _currentGrammar.contentMode;
94     }
95   }
96 }