diff options
Diffstat (limited to 'pkg/tpl')
-rw-r--r-- | pkg/tpl/add.templ | 24 | ||||
-rw-r--r-- | pkg/tpl/add_templ.go | 52 | ||||
-rw-r--r-- | pkg/tpl/layout.templ | 8 | ||||
-rw-r--r-- | pkg/tpl/layout_templ.go | 10 | ||||
-rw-r--r-- | pkg/tpl/list.templ (renamed from pkg/tpl/index.templ) | 10 | ||||
-rw-r--r-- | pkg/tpl/list_templ.go (renamed from pkg/tpl/index_templ.go) | 15 | ||||
-rw-r--r-- | pkg/tpl/quote.templ | 17 | ||||
-rw-r--r-- | pkg/tpl/quote_templ.go | 48 | ||||
-rw-r--r-- | pkg/tpl/random.templ | 27 | ||||
-rw-r--r-- | pkg/tpl/random_templ.go | 37 | ||||
-rw-r--r-- | pkg/tpl/rate.templ | 31 | ||||
-rw-r--r-- | pkg/tpl/rate_templ.go | 85 |
12 files changed, 308 insertions, 56 deletions
diff --git a/pkg/tpl/add.templ b/pkg/tpl/add.templ index 14b57eb..82d76e5 100644 --- a/pkg/tpl/add.templ +++ b/pkg/tpl/add.templ @@ -2,7 +2,10 @@ package tpl import "fmt" +var captchaHandler = templ.NewOnceHandle() + templ AddQuotePage(form *AddQuoteForm, err string) { + {{ captchaURL := fmt.Sprintf("/captcha/download/%s.png", form.CaptchaID) }} @Layout(HeaderParams{}) { <h2>Добавление цитаты</h2> if err != "" { @@ -14,10 +17,27 @@ templ AddQuotePage(form *AddQuoteForm, err string) { <form method="post"> <textarea rows="5" name="quote" placeholder="Текст цитаты">{ form.Quote }</textarea> <input type="hidden" name="captcha_id" value={ form.CaptchaID }/> - <img class="captcha" src={ fmt.Sprintf("/captcha/download/%s.png", form.CaptchaID) }/> - <input type="text" name="captcha_value" placeholder="Код с картинки"/> + <label for="captcha_value"> + <img class="captcha" id="captcha" src={ captchaURL }/> + <a + role="button" + data-url={ captchaURL } + onclick="reloadCaptcha(this)" + > + <i class="fa fa-refresh"></i> Обновить капчу + </a> + </label> + <input type="text" name="captcha_value" id="captcha_value" placeholder="Код с картинки"/> <input type="submit" value="Отправить на модерацию"/> </form> + @captchaHandler.Once() { + <script type="text/javascript"> + function reloadCaptcha(event) { + const url = event.getAttribute('data-url'); + document.getElementById('captcha').setAttribute('src', url+'?reload='+Math.random()); + } + </script> + } } } diff --git a/pkg/tpl/add_templ.go b/pkg/tpl/add_templ.go index d47582b..b0731ac 100644 --- a/pkg/tpl/add_templ.go +++ b/pkg/tpl/add_templ.go @@ -10,6 +10,8 @@ import templruntime "github.com/a-h/templ/runtime" import "fmt" +var captchaHandler = templ.NewOnceHandle() + func AddQuotePage(form *AddQuoteForm, err string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context @@ -31,6 +33,7 @@ func AddQuotePage(form *AddQuoteForm, err string) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) + captchaURL := fmt.Sprintf("/captcha/download/%s.png", form.CaptchaID) templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -55,7 +58,7 @@ func AddQuotePage(form *AddQuoteForm, err string) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(err) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 11, Col: 9} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 14, Col: 9} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -73,7 +76,7 @@ func AddQuotePage(form *AddQuoteForm, err string) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(form.Quote) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 15, Col: 85} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 18, Col: 85} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -86,26 +89,61 @@ func AddQuotePage(form *AddQuoteForm, err string) templ.Component { var templ_7745c5c3_Var5 string templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(form.CaptchaID) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 16, Col: 64} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 19, Col: 64} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"> <img class=\"captcha\" src=\"") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"> <label for=\"captcha_value\"><img class=\"captcha\" id=\"captcha\" src=\"") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var6 string - templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/captcha/download/%s.png", form.CaptchaID)) + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(captchaURL) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 17, Col: 85} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 21, Col: 54} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"> <input type=\"text\" name=\"captcha_value\" placeholder=\"Код с картинки\"> <input type=\"submit\" value=\"Отправить на модерацию\"></form>") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"> <a role=\"button\" data-url=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(captchaURL) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/add.templ`, Line: 24, Col: 26} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" onclick=\"reloadCaptcha(this)\"><i class=\"fa fa-refresh\"></i> Обновить капчу</a></label> <input type=\"text\" name=\"captcha_value\" id=\"captcha_value\" placeholder=\"Код с картинки\"> <input type=\"submit\" value=\"Отправить на модерацию\"></form>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var8 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script type=\"text/javascript\">\n\t\t\t\tfunction reloadCaptcha(event) {\n\t\t\t\t\tconst url = event.getAttribute('data-url');\n\t\t\t\t\tdocument.getElementById('captcha').setAttribute('src', url+'?reload='+Math.random());\n\t\t\t\t}\n\t\t\t</script>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = captchaHandler.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var8), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/pkg/tpl/layout.templ b/pkg/tpl/layout.templ index c777457..d96aa50 100644 --- a/pkg/tpl/layout.templ +++ b/pkg/tpl/layout.templ @@ -10,9 +10,13 @@ templ Layout(params HeaderParams) { <link rel="stylesheet" href="/css/pico.css"/> <link rel="stylesheet" href="/css/style.css"/> <link rel="stylesheet" href="/css/fork-awesome.min.css"/> + <link rel="alternate" type="application/rss+xml" title="RSS feed" href="/feed/rss"> + <link rel="alternate" type="application/atom+xml" title="ATOM feed" href="/feed/atom"> + <link rel="alternate" type="application/json" title="json feed" href="/feed/json"> <meta property="og:title" content={ params.Title }/> <meta property="og:url" content={ params.URL }/> <meta property="og:description" content={ params.Description }/> + <meta name="yandex-verification" content="ee0e23da00ce9fe4" /> <title>ШОргРу</title> </head> <body> @@ -21,9 +25,11 @@ templ Layout(params HeaderParams) { <ul> <li><a href="/"><strong>ШОргРу</strong></a></li> </ul> - <ul> + <ul hx-boost="true" hx-indicator=".loader"> + <span aria-busy="true" class="loader htmx-indicator">Загрузка...</span> <li><a href="/">Главная</a></li> <li><a href="/random">Случайные</a></li> + <li><a href="/top">Топ</a></li> <li><a href="/add">Добавить цитату</a></li> </ul> </nav> diff --git a/pkg/tpl/layout_templ.go b/pkg/tpl/layout_templ.go index a86bb30..10b0bfc 100644 --- a/pkg/tpl/layout_templ.go +++ b/pkg/tpl/layout_templ.go @@ -29,14 +29,14 @@ func Layout(params HeaderParams) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"color-scheme\" content=\"light dark\"><link rel=\"stylesheet\" href=\"/css/pico.css\"><link rel=\"stylesheet\" href=\"/css/style.css\"><link rel=\"stylesheet\" href=\"/css/fork-awesome.min.css\"><meta property=\"og:title\" content=\"") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"color-scheme\" content=\"light dark\"><link rel=\"stylesheet\" href=\"/css/pico.css\"><link rel=\"stylesheet\" href=\"/css/style.css\"><link rel=\"stylesheet\" href=\"/css/fork-awesome.min.css\"><link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS feed\" href=\"/feed/rss\"><link rel=\"alternate\" type=\"application/atom+xml\" title=\"ATOM feed\" href=\"/feed/atom\"><link rel=\"alternate\" type=\"application/json\" title=\"json feed\" href=\"/feed/json\"><meta property=\"og:title\" content=\"") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(params.Title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 13, Col: 51} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 16, Col: 51} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -49,7 +49,7 @@ func Layout(params HeaderParams) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(params.URL) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 14, Col: 47} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 17, Col: 47} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -62,13 +62,13 @@ func Layout(params HeaderParams) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(params.Description) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 15, Col: 63} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/layout.templ`, Line: 18, Col: 63} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><title>ШОргРу</title></head><body><main class=\"container\"><nav><ul><li><a href=\"/\"><strong>ШОргРу</strong></a></li></ul><ul><li><a href=\"/\">Главная</a></li><li><a href=\"/random\">Случайные</a></li><li><a href=\"/add\">Добавить цитату</a></li></ul></nav>") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><meta name=\"yandex-verification\" content=\"ee0e23da00ce9fe4\"><title>ШОргРу</title></head><body><main class=\"container\"><nav><ul><li><a href=\"/\"><strong>ШОргРу</strong></a></li></ul><ul hx-boost=\"true\" hx-indicator=\".loader\"><span aria-busy=\"true\" class=\"loader htmx-indicator\">Загрузка...</span><li><a href=\"/\">Главная</a></li><li><a href=\"/random\">Случайные</a></li><li><a href=\"/top\">Топ</a></li><li><a href=\"/add\">Добавить цитату</a></li></ul></nav>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/pkg/tpl/index.templ b/pkg/tpl/list.templ index 3655531..014a2db 100644 --- a/pkg/tpl/index.templ +++ b/pkg/tpl/list.templ @@ -2,21 +2,25 @@ package tpl import ( "fmt" + "sh.org.ru/pkg/config" "sh.org.ru/pkg/model" "strconv" + "sh.org.ru/pkg/middleware" ) -templ Index(quotes []model.Quote, page, count int) { +templ List(quotes []model.Quote, page, count int) { + {{ host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host }} @Layout(HeaderParams{ Title: "Цитатник Рунета", Description: "Новый цитатник Рунета", - URL: "https://sh.org.ru/", + URL: host, }) { for _, q := range quotes { @Quote(&q) } + <span aria-busy="true" class="loader htmx-indicator">Загрузка...</span> <nav> - <ul hx-boost="true"> + <ul hx-boost="true" hx-indicator=".loader"> if page > 0 { <li><a href={ templ.URL(fmt.Sprintf("/?page=%d", page-1)) }>←</a></li> } diff --git a/pkg/tpl/index_templ.go b/pkg/tpl/list_templ.go index eabd30d..2ed9497 100644 --- a/pkg/tpl/index_templ.go +++ b/pkg/tpl/list_templ.go @@ -10,11 +10,13 @@ import templruntime "github.com/a-h/templ/runtime" import ( "fmt" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/middleware" "sh.org.ru/pkg/model" "strconv" ) -func Index(quotes []model.Quote, page, count int) templ.Component { +func List(quotes []model.Quote, page, count int) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -35,6 +37,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) + host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -53,7 +56,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { return templ_7745c5c3_Err } } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <nav><ul hx-boost=\"true\">") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <span aria-busy=\"true\" class=\"loader htmx-indicator\">Загрузка...</span><nav><ul hx-boost=\"true\" hx-indicator=\".loader\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -86,7 +89,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(p) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/index.templ`, Line: 27, Col: 14} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/list.templ`, Line: 31, Col: 14} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -113,7 +116,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { var templ_7745c5c3_Var6 string templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(p) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/index.templ`, Line: 29, Col: 64} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/list.templ`, Line: 33, Col: 64} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { @@ -147,7 +150,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(count)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/index.templ`, Line: 38, Col: 34} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/list.templ`, Line: 42, Col: 34} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { @@ -162,7 +165,7 @@ func Index(quotes []model.Quote, page, count int) templ.Component { templ_7745c5c3_Err = Layout(HeaderParams{ Title: "Цитатник Рунета", Description: "Новый цитатник Рунета", - URL: "https://sh.org.ru/", + URL: host, }).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err diff --git a/pkg/tpl/quote.templ b/pkg/tpl/quote.templ index 34429d1..7b38faa 100644 --- a/pkg/tpl/quote.templ +++ b/pkg/tpl/quote.templ @@ -2,11 +2,14 @@ package tpl import ( "fmt" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/middleware" "sh.org.ru/pkg/model" "strconv" ) templ Quote(quote *model.Quote) { + {{ host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host }} <article> <header> <a href={ templ.URL(fmt.Sprintf("/quote/%d", quote.ID)) }>#{ strconv.Itoa(int(quote.ID)) }</a> @@ -14,24 +17,24 @@ templ Quote(quote *model.Quote) { </header> @templ.Raw(quote.Text()) <footer> - <span> - <a target="_blank" href={ templ.URL(fmt.Sprintf("https://t.me/share/url?url=https://sh.org.ru/quote/%d", quote.ID)) }><i class="fa fa-telegram" aria-hidden="true"></i></a> ·  - <a target="_blank" href={ templ.URL(fmt.Sprintf("https://vk.com/share.php?url=https://sh.org.ru/quote/%d", quote.ID)) }><i class="fa fa-vk" aria-hidden="true"></i></a> ·  - <a target="_blank" href={ templ.URL(fmt.Sprintf("https://connect.ok.ru/offer?url=https://sh.org.ru/quote/%d", quote.ID)) }><i class="fa fa-odnoklassniki-square" aria-hidden="true"></i></a> - </span> + @Rate(quote, 0) <span> if quote.Archive { <abbr title="Цитата из старого цитатника">Архив</abbr> } + <a target="_blank" href={ templ.URL(fmt.Sprintf("https://t.me/share/url?url=%s/quote/%d", host, quote.ID)) }><i class="fa fa-telegram" aria-hidden="true"></i></a> ·  + <a target="_blank" href={ templ.URL(fmt.Sprintf("https://vk.com/share.php?url=%s/quote/%d", host, quote.ID)) }><i class="fa fa-vk" aria-hidden="true"></i></a> ·  + <a target="_blank" href={ templ.URL(fmt.Sprintf("https://connect.ok.ru/offer?url=%s/quote/%d", host, quote.ID)) }><i class="fa fa-odnoklassniki-square" aria-hidden="true"></i></a> </span> </footer> </article> } templ QuotePage(quote *model.Quote) { + {{ host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host }} @Layout(HeaderParams{ - Title: "Цитата #" + strconv.Itoa(int(quote.ID)), - URL: fmt.Sprintf("https://sh.org.ru/quote/%d", quote.ID), + Title: "Цитата #" + strconv.Itoa(int(quote.ID)), + URL: fmt.Sprintf("%s/quote/%d", host, quote.ID), Description: templ.EscapeString(quote.Quote), }) { @Quote(quote) diff --git a/pkg/tpl/quote_templ.go b/pkg/tpl/quote_templ.go index fa139cd..cc799cc 100644 --- a/pkg/tpl/quote_templ.go +++ b/pkg/tpl/quote_templ.go @@ -10,6 +10,8 @@ import templruntime "github.com/a-h/templ/runtime" import ( "fmt" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/middleware" "sh.org.ru/pkg/model" "strconv" ) @@ -35,6 +37,7 @@ func Quote(quote *model.Quote) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) + host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<article><header><a href=\"") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err @@ -51,7 +54,7 @@ func Quote(quote *model.Quote) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(int(quote.ID))) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/quote.templ`, Line: 12, Col: 91} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/quote.templ`, Line: 15, Col: 91} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -64,7 +67,7 @@ func Quote(quote *model.Quote) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(quote.CreatedAt.Format("02.01.06")) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/quote.templ`, Line: 13, Col: 92} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/quote.templ`, Line: 16, Col: 92} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -78,11 +81,29 @@ func Quote(quote *model.Quote) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<footer><span><a target=\"_blank\" href=\"") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<footer>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var5 templ.SafeURL = templ.URL(fmt.Sprintf("https://t.me/share/url?url=https://sh.org.ru/quote/%d", quote.ID)) + templ_7745c5c3_Err = Rate(quote, 0).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if quote.Archive { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<abbr title=\"Цитата из старого цитатника\">Архив</abbr> ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a target=\"_blank\" href=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 templ.SafeURL = templ.URL(fmt.Sprintf("https://t.me/share/url?url=%s/quote/%d", host, quote.ID)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var5))) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err @@ -91,7 +112,7 @@ func Quote(quote *model.Quote) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var6 templ.SafeURL = templ.URL(fmt.Sprintf("https://vk.com/share.php?url=https://sh.org.ru/quote/%d", quote.ID)) + var templ_7745c5c3_Var6 templ.SafeURL = templ.URL(fmt.Sprintf("https://vk.com/share.php?url=%s/quote/%d", host, quote.ID)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var6))) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err @@ -100,22 +121,12 @@ func Quote(quote *model.Quote) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var7 templ.SafeURL = templ.URL(fmt.Sprintf("https://connect.ok.ru/offer?url=https://sh.org.ru/quote/%d", quote.ID)) + var templ_7745c5c3_Var7 templ.SafeURL = templ.URL(fmt.Sprintf("https://connect.ok.ru/offer?url=%s/quote/%d", host, quote.ID)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var7))) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><i class=\"fa fa-odnoklassniki-square\" aria-hidden=\"true\"></i></a></span> <span>") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if quote.Archive { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<abbr title=\"Цитата из старого цитатника\">Архив</abbr>") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span></footer></article>") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><i class=\"fa fa-odnoklassniki-square\" aria-hidden=\"true\"></i></a></span></footer></article>") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -144,6 +155,7 @@ func QuotePage(quote *model.Quote) templ.Component { templ_7745c5c3_Var8 = templ.NopComponent } ctx = templ.ClearChildren(ctx) + host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host templ_7745c5c3_Var9 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -164,7 +176,7 @@ func QuotePage(quote *model.Quote) templ.Component { }) templ_7745c5c3_Err = Layout(HeaderParams{ Title: "Цитата #" + strconv.Itoa(int(quote.ID)), - URL: fmt.Sprintf("https://sh.org.ru/quote/%d", quote.ID), + URL: fmt.Sprintf("%s/quote/%d", host, quote.ID), Description: templ.EscapeString(quote.Quote), }).Render(templ.WithChildren(ctx, templ_7745c5c3_Var9), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { diff --git a/pkg/tpl/random.templ b/pkg/tpl/random.templ index 39ce3ff..2d25a03 100644 --- a/pkg/tpl/random.templ +++ b/pkg/tpl/random.templ @@ -1,12 +1,33 @@ package tpl import ( + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/middleware" "sh.org.ru/pkg/model" ) templ Random(quotes []model.Quote) { - for _, q := range quotes { - @Quote(&q) + {{ host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host }} + @Layout(HeaderParams{ + Title: "Цитатник Рунета", + Description: "Новый цитатник Рунета", + URL: host, + }) { + <div id="random"> + for _, q := range quotes { + @Quote(&q) + } + <a + role="button" + hx-get="/random" + hx-swap="outerHTML" + hx-select="#random" + hx-target="#random" + hx-indicator="#loader" + > + Загрузить ещё... + </a> + <span aria-busy="true" id="loader" class="htmx-indicator">Загрузка...</span> + </div> } - <a role="button" hx-get="/random" hx-swap="outerHTML">Загрузить ещё...</a> } diff --git a/pkg/tpl/random_templ.go b/pkg/tpl/random_templ.go index 79f5685..803bf22 100644 --- a/pkg/tpl/random_templ.go +++ b/pkg/tpl/random_templ.go @@ -9,6 +9,8 @@ import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" import ( + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/middleware" "sh.org.ru/pkg/model" ) @@ -33,13 +35,40 @@ func Random(quotes []model.Quote) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - for _, q := range quotes { - templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer) + host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host + templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div id=\"random\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a role=\"button\" hx-get=\"/random\" hx-swap=\"outerHTML\">Загрузить ещё...</a>") + for _, q := range quotes { + templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a role=\"button\" hx-get=\"/random\" hx-swap=\"outerHTML\" hx-select=\"#random\" hx-target=\"#random\" hx-indicator=\"#loader\">Загрузить ещё...</a> <span aria-busy=\"true\" id=\"loader\" class=\"htmx-indicator\">Загрузка...</span></div>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = Layout(HeaderParams{ + Title: "Цитатник Рунета", + Description: "Новый цитатник Рунета", + URL: host, + }).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/pkg/tpl/rate.templ b/pkg/tpl/rate.templ new file mode 100644 index 0000000..bd63df8 --- /dev/null +++ b/pkg/tpl/rate.templ @@ -0,0 +1,31 @@ +package tpl + +import ( + "fmt" + "sh.org.ru/pkg/model" + "strconv" +) + +templ Rate(quote *model.Quote, act int) { + <nav class="rate"> + <a + hx-post={ fmt.Sprintf("/rate/%d", quote.ID) } + hx-target="closest .rate" + hx-vals='{"vote": "up"}' + href="#" + > + <i class="fa fa-plus"></i> + </a> + + { strconv.Itoa(quote.Rating) } + + <a + hx-post={ fmt.Sprintf("/rate/%d", quote.ID) } + hx-target="closest .rate" + hx-vals='{"vote": "down"}' + href="#" + > + <i class="fa fa-minus"></i> + </a> + </nav> +} diff --git a/pkg/tpl/rate_templ.go b/pkg/tpl/rate_templ.go new file mode 100644 index 0000000..6c27477 --- /dev/null +++ b/pkg/tpl/rate_templ.go @@ -0,0 +1,85 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.778 +package tpl + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +import ( + "fmt" + "sh.org.ru/pkg/model" + "strconv" +) + +func Rate(quote *model.Quote, act int) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"rate\"><a hx-post=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/rate/%d", quote.ID)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/rate.templ`, Line: 12, Col: 46} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" hx-target=\"closest .rate\" hx-vals=\"{"vote": "up"}\" href=\"#\"><i class=\"fa fa-plus\"></i></a> ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(quote.Rating)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/rate.templ`, Line: 20, Col: 30} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" <a hx-post=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/rate/%d", quote.ID)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/rate.templ`, Line: 23, Col: 46} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" hx-target=\"closest .rate\" hx-vals=\"{"vote": "down"}\" href=\"#\"><i class=\"fa fa-minus\"></i></a></nav>") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +var _ = templruntime.GeneratedTemplate |