Ethosa пре 5 година
родитељ
комит
64a1b41187
5 измењених фајлова са 49 додато и 27 уклоњено
  1. 1 1
      README.md
  2. 1 1
      akane.nimble
  3. 40 24
      akane/akane.nim
  4. 2 1
      tests/test4/main.nim
  5. 5 0
      tests/test4/templates/index.html

+ 1 - 1
README.md

@@ -5,7 +5,7 @@
 [![Nim language-plastic](https://github.com/Ethosa/yukiko/blob/master/nim-lang.svg)](https://github.com/Ethosa/yukiko/blob/master/nim-lang.svg)
 [![License](https://img.shields.io/github/license/Ethosa/akane)](https://github.com/Ethosa/akane/blob/master/LICENSE)
 
-<h4>Latest version - 0.0.4</h4>
+<h4>Latest version - 0.0.5</h4>
 <h4>Stable version - ?</h4>
 </div>
 

+ 1 - 1
akane.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "akane"
 author = "Ethosa"
-version = "0.0.4"
+version = "0.0.5"
 description = "The Nim asynchronous web framework."
 license = "MIT"
 srcDir = "akane"

+ 40 - 24
akane/akane.nim

@@ -56,42 +56,58 @@ proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async,
   ## Arguments:
   ## -   ``name`` - template's name, e.g. "index", "api", etc.
   ## -   ``json`` - Json data, which replaces in the template.
+  ##
+  ## Replaces:
+  ## -  $(key) -> value
+  ## -  if $(key) { ... } -> ... (if value is true)
   var
     file = openAsync(("templates" / name) & ".html")
     readed = await file.readAll()
   file.close()
   for key, value in json.pairs:
+    # ---- regex patterns ---- #
     let
-      # if statment, e.g.: if $(variable) {......}
-      if_stmt = re("if\\s*(\\$\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
       # variable statment, e.g.: $(variable)
       variable_stmt = re("(\\$\\s*\\(" & key & "\\))")
+      # if statment, e.g.: if $(variable) {......}
+      if_stmt = re("if\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
+      # if not statment, e.g.: if not $(variable) {......}
+      if_notstmt = re("if\\s*not\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
+
+    # ---- converts value to bool ---- #
+    var value_bool = false
+    case value.kind:
+    of JBool:
+      if value.getBool:
+        value_bool = true
+    of JInt:
+      if value.getInt != 0:
+        value_bool = true
+    of JFloat:
+      if value.getFloat != 0.0:
+        value_bool = true
+    of JString:
+      if value.getStr.len > 0:
+        value_bool = true
+    of JArray:
+      if value.len > 0:
+        value_bool = true
+    of JObject:
+      if value.getFields.len > 0:
+        value_bool = true
+    else: discard
+
+    # ---- replace ----- #
     if readed.contains(if_stmt):
-      var canplace: bool = false
-      case value.kind:
-      of JBool:
-        if value.getBool:
-          canplace = true
-      of JInt:
-        if value.getInt != 0:
-          canplace = true
-      of JFloat:
-        if value.getFloat != 0.0:
-          canplace = true
-      of JString:
-        if value.getStr.len > 0:
-          canplace = true
-      of JArray:
-        if value.len > 0:
-          canplace = true
-      of JObject:
-        if value.getFields.len > 0:
-          canplace = true
-      else: discard
-      if canplace:
+      if value_bool:
         readed = readed.replacef(if_stmt, "$2")
       else:
         readed = readed.replacef(if_stmt, "")
+    if readed.contains(if_notstmt):
+      if value_bool:
+        readed = readed.replacef(if_notstmt, "")
+      else:
+        readed = readed.replacef(if_notstmt, "$2")
     readed = readed.replacef(variable_stmt, $value)
   return readed
 

+ 2 - 1
tests/test4/main.nim

@@ -8,7 +8,8 @@ proc main =  # main proc for gcsafe
     server = newServer(debug=true)
     data: JsonNode = %{
       "myvariable": %0,
-      "can_place": %false
+      "can_place": %false,
+      "test": %2
     }
 
   server.pages:

+ 5 - 0
tests/test4/templates/index.html

@@ -5,6 +5,11 @@
 </head>
 <body>
   <h1 align="center">Now the value is $(myvariable)</h1>
+  if not $(can_place)
+  {
+    <h1 align="center">Can't place</h1>
+  }
+
   if $(can_place)
   {
     <h1 align="center">Can place is "true"</h1>