Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new release v3.12.0 #292

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions examples/website/src/components/about_section.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import
../ui/colors


type Data = seq[tuple[name, url: string]]


proc AboutSection*(name: string, data: Data): TagRef =
proc AboutSection*(name: string, data: seq[tuple[name, url: string]]): TagRef =
buildHtml:
tDiv(class = "flex flex-col gap-8 lg:gap-4"):
tP(class = "font-semibold text-2xl lg:text-base"):
Expand All @@ -16,4 +13,4 @@ proc AboutSection*(name: string, data: Data): TagRef =
for item in data:
# Section link
tA(href = item.url, class = "text-[{LinkForeground}] visited:text-[{LinkVisitedForeground}] hover:text-[{LinkActiveForeground}] translation-all"):
{name}
{item.name}
8 changes: 7 additions & 1 deletion examples/website/src/docs/introduction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ component Introduction:
tH3:
{translate"Contributors"}
" *"
tDiv(class = "grid grid-cols-5 lg:grid-cols-10 xl:grid-cols-15 gap-x-2 gap-y-12"):
tDiv(class = "grid grid-cols-5 lg:grid-cols-10 xl:grid-cols-8 gap-x-2 gap-y-12"):
Contributor("quimt", "https://github.com/quimt", "https://avatars.githubusercontent.com/u/126020181?v=4")
Contributor("its5Q", "https://github.com/its5Q", "https://avatars.githubusercontent.com/u/12975646?v=4")
Contributor("Lum", "https://github.com/not-lum", "https://avatars.githubusercontent.com/u/62594565?v=4")
Expand All @@ -163,6 +163,12 @@ component Introduction:
Contributor("Александр Старочкин", "https://github.com/levovix0", "https://avatars.githubusercontent.com/u/53170138?v=4")
Contributor("MouriKogorou", "https://github.com/MouriKogorou", "https://avatars.githubusercontent.com/u/43428806?v=4")
Contributor("Devon", "https://github.com/winrid", "https://avatars.githubusercontent.com/u/1733933?v=4")
Contributor("Yuriy Balyuk", "https://github.com/veksha", "https://avatars.githubusercontent.com/u/275333?v=4")
Contributor("Damian Zaręba", "https://github.com/KhazAkar", "https://avatars.githubusercontent.com/u/12693890?v=4")
Contributor("bluemax75", "https://github.com/bluemax75", "https://avatars.githubusercontent.com/u/11153375?v=4")
Contributor("svbalogh", "https://github.com/svbalogh", "https://avatars.githubusercontent.com/u/46842029?v=4")
Contributor("MelonCodeUK", "https://github.com/MelonCodeUK", "https://avatars.githubusercontent.com/u/138726110?v=4")
Contributor("baseplate-admin", "https://github.com/baseplate-admin", "https://avatars.githubusercontent.com/u/61817579?v=4")
tP(class = "text-base lg:text-sm xl:text-xs pt-4"):
"* "
{translate"This includes all members of the HappyX community who have contributed to its development (issues and pull requests)"}
2 changes: 1 addition & 1 deletion examples/website/src/pages/home.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mount Home:
{translate"modern and powerful ready-to-product web framework"}
tDiv(class = "cursor-pointer hover:scale-110 hover:z-10 transition-all duration-500 flex flex-col rounded-xl px-6 py-4 drop-shadow-2xl bg-gradient-to-r from-[{Background}] to-[{BackgroundSecondary}] dark:from-[{BackgroundDark}] dark:to-[{BackgroundSecondaryDark}]"):
tP(class = "text-5xl lg:text-xl xl:text-3xl font-semibold"):
{translate"metaprogramming"}
"metaprogramming"
tP(class = "text-3xl lg:text-base"):
{translate"HappyX based on metaprogramming"}
tP(class = "text-3xl lg:text-base"):
Expand Down
5 changes: 5 additions & 0 deletions src/happyx/private/macro_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,11 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
"is", statement[0], ident"TagRef"
),
statement[0]
), newNimNode(nnkElifBranch).add(
newCall("is", statement[0], newNimNode(nnkProcTy)),
block:
var call = newCall(statement[0])
call
), newNimNode(nnkElse).add(
newCall("initTag", newCall("$", statement[0]), newLit(true))
)))
Expand Down
27 changes: 27 additions & 0 deletions src/happyx/spa/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,33 @@ macro buildHtml*(html: untyped): untyped =
result.add(newLit(true))


template thunkHtml*(body: untyped): proc(): TagRef =
proc(): TagRef = buildHtml:
body


template thunkHtmls*(body: untyped): seq[proc(): TagRef] =
block:
var res: seq[proc(): TagRef]
template html(b: untyped) =
res.add:
proc(): TagRef = buildHtml:
b
body
res


template buildHtmls*(body: untyped): seq[TagRef] =
block:
var res: seq[TagRef]
template html(b: untyped) =
res.add:
buildHtml:
b
body
res


when enableDefaultComponents:
macro buildHtmlSlot*(html: untyped, inCycle, inComponent: static[bool]): untyped =
## `buildHtml` macro provides building HTML tags with YAML-like syntax.
Expand Down
75 changes: 42 additions & 33 deletions src/happyx/spa/translatable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,67 +112,76 @@ macro translatable*(body: untyped): untyped =
return translatesStatement


macro translate*(self: string): string =
macro translate*(self: string, variables: varargs[string]): string =
## Translates `self` string to current client language (SPA) or accept-language header (SSG/SSR)
let
language =
newNimNode(nnkIfExpr).add(
newNimNode(nnkElifBranch).add(
when defined(js):
newCall("==", newDotExpr(newDotExpr(ident"languageSettings", ident"val"), ident"lang"), newLit"auto")
else:
newCall("==", newDotExpr(ident"languageSettings", ident"lang"), newLit"auto"),
when defined(js):
newDotExpr(ident"navigator", ident"language")
else:
ident"acceptLanguage"
), newNimNode(nnkElse).add(
when defined(js):
newDotExpr(newDotExpr(ident"languageSettings", ident"val"), ident"lang")
else:
newDotExpr(ident"languageSettings", ident"lang")
)
newCall("[]", newCall("$",
newNimNode(nnkIfExpr).add(
newNimNode(nnkElifBranch).add(
when defined(js):
newCall("==", newDotExpr(newDotExpr(ident"languageSettings", ident"val"), ident"lang"), newLit"auto")
else:
newCall("==", newDotExpr(ident"languageSettings", ident"lang"), newLit"auto"),
when defined(js):
newDotExpr(ident"navigator", ident"language")
else:
ident"acceptLanguage"
), newNimNode(nnkElse).add(
when defined(js):
newDotExpr(newDotExpr(ident"languageSettings", ident"val"), ident"lang")
else:
newDotExpr(ident"languageSettings", ident"lang")
)
)),
newCall("..", newLit(0), newLit(1))
)
source =
sourceRaw =
when self is static[string]:
newLit(self)
else:
self
translations = ident"translates"
source = newCall("format", newNimNode(nnkBracketExpr).add(
newNimNode(nnkBracketExpr).add(
translations, sourceRaw
),
language
))
sourceDefault = newCall("format", newNimNode(nnkBracketExpr).add(
newNimNode(nnkBracketExpr).add(
translations, sourceRaw
),
newLit"default"
))

for i in variables:
source.add(i)
sourceDefault.add(i)

result = newNimNode(nnkIfStmt).add(
newNimNode(nnkElifBranch).add(
newCall("not",
newCall(
"hasKey",
newNimNode(nnkBracketExpr).add(
translations, source
translations, sourceRaw
),
language
)
),
newNimNode(nnkBracketExpr).add(
newNimNode(nnkBracketExpr).add(
translations, source
),
newLit"default"
)
sourceDefault
), newNimNode(nnkElse).add(
newNimNode(nnkBracketExpr).add(
newNimNode(nnkBracketExpr).add(
translations, source
),
language
)
source
)
)
when not (self is static[string]):
result.insert(0, newNimNode(nnkElifBranch).add(
newCall("not",
newCall(
"hasKey",
translations, source
translations, sourceRaw
)
),
source
sourceRaw
))
37 changes: 23 additions & 14 deletions src/happyx/ssr/docs/autodocs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,24 @@ proc openApiDocs*(docsData: NimNode): NimNode =
for m in text.findAll(re2"(?m)^\s*(\w[\w\d_]*)\s*=\s*([^\n]+)$"):
pathData[text[m.group(0)]] = %text[m.group(1)]
# Params
for p in route.pathParams:
let param = %*{
"name": p.name,
"required": not p.optional,
"in": "path",
"schema": {
"type": p.paramType
}
}
pathData["parameters"].add(param)

var paramMatches: RegexMatch2
if text.find(re2"@params\s*{((\s*\w[\w\d]*\!?\s*(:\s*\w+)?[^\n]+)+)\s*}", paramMatches):
let paramText = text[paramMatches.group(1)]
let paramText = text[paramMatches.group(0)]
for m in paramText.findAll(
re2"(?m)^\s*(\w[\w\d_]*)(!)?\s*(:\s*\w[\w\d]*)?(\s*\-\s*[^\n]+)?"
):
pathData["parameters"].add(%*{
let param = %*{
"name": paramText[m.group(0)],
"required": m.group(1).len != 0,
"description":
Expand All @@ -365,18 +376,16 @@ proc openApiDocs*(docsData: NimNode): NimNode =
else:
"string"
}
})

for p in route.pathParams:
let param = %*{
"name": p.name,
"required": not p.optional,
"in": "path",
"schema": {
"type": p.paramType
}
}
pathData["parameters"].add(param)
}
var hasParam = false
for p in 0..<pathData["parameters"].len:
if pathData["parameters"][p]["name"] == param["name"]:
pathData["parameters"][p]["schema"] = param["schema"]
pathData["parameters"][p]["description"] = param["description"]
hasParam = true
break
if not hasParam:
pathData["parameters"].add(param)

for m in route.models:
echo m
Expand Down
12 changes: 12 additions & 0 deletions tests/index18.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="app"></div>
<div>
</div>
<script src="testjs18.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions tests/testc2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ serve "127.0.0.1", 5000:
var counter = 0

get "/{title:string}/{left:float}/{right:float}":
## Calculate left and right. Shows title.
##
## @openapi {
## operationId = calculate
## summary = calculate left and right.
##
## @params {
## title: string - just title
## left: integer - left number
## right: integer - right number
## }
##
## @responses {
## asdad
## }
## }
req.answerHtml render(title, left, right)

get "/":
Expand Down
34 changes: 34 additions & 0 deletions tests/testjs18.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ../src/happyx

let htmlProcs = thunkHtmls:
html:
tSpan: "front"
html:
tSpan: "back"

let htmlTags = buildHtmls:
html:
tSpan: "right"
html:
tSpan: "left"

component FormatProcHtml:
p: (proc (): TagRef) # parentheses needed in avoid "nested statements" error
html:
em(style="color:blue"):
{self.p()}

component FormatTagHtml:
t: TagRef
html:
em(style="color:blue"):
{self.t.val}

appRoutes("app"):
"/":
for i in 1..5:
tDiv:{$htmlProcs[0]()}
FormatProcHtml(htmlProcs[1])
for i in 1..5:
tDiv:{$htmlTags[0]}
FormatTagHtml(htmlTags[1])
13 changes: 8 additions & 5 deletions tests/testjs9.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import


translatable:
"Hello":
# "Hello!" by default
"ru" -> "Привет"
"fr" -> "Bonjour"
"hello":
# "Hello, $#!" by default
"default" -> "Hello, $#!"
"ru" -> "Привет, $#!"
"fr" -> "Bonjour, $#!"

echo translates

appRoutes("app"):
"/":
component Pure
tDiv:
"Hello"
{translate"nothing"}
nim:
var
x = 0
str = translate("Hello")
str = translate("hello", "username")
tDiv(class = "flex flex-col gap-2"):
while x <= 20:
tDiv(class = "flex gap-2"):
Expand Down