2022-04-09 08:28:57 +00:00
package help
// stuff.go is used for meta stuff about the wiki or all hyphae at once.
import (
"github.com/bouncepaw/mycomarkup/v4"
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/viewutil"
2022-05-18 16:58:24 +00:00
"github.com/gorilla/mux"
2022-04-09 08:28:57 +00:00
"io"
"net/http"
"strings"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
)
var (
chain viewutil . Chain
ruTranslation = `
{ { define "title" } } Справка { { end } }
{ { define "entry not found" } } Статья не найдена { { end } }
{ { define "entry not found invitation" } } Если вы хотите написать эту статью сами , то будем рады вашим правкам < a class = "wikilink wikilink_external wikilink_https" href = "https://github.com/bouncepaw/mycorrhiza" > в репозитории Миокризы < / a > . { { end } }
2022-04-09 11:55:55 +00:00
{ { define "topics" } } Темы справки { { end } }
{ { define "main" } } Введение { { end } }
{ { define "hypha" } } Гифа { { end } }
{ { define "media" } } Медиа { { end } }
{ { define "mycomarkup" } } Микоразметка { { end } }
{ { define "category" } } Категории { { end } }
{ { define "interface" } } Интерфейс { { end } }
{ { define "prevnext" } } Пред / след { { end } }
{ { define "top_bar" } } Верхняя панель { { end } }
{ { define "sibling_hyphae" } } Гифы - сиблинги { { end } }
{ { define "special pages" } } Специальные страницы { { end } }
{ { define "recent_changes" } } Недавние изменения { { end } }
{ { define "feeds" } } Ленты { { end } }
2022-05-06 20:30:48 +00:00
{ { define "orphans" } } Гифы - сироты { { end } }
2022-04-09 11:55:55 +00:00
{ { define "configuration" } } Конфигурация ( для администраторов ) { { end } }
{ { define "config_file" } } Файл конфигурации { { end } }
{ { define "lock" } } Замок { { end } }
{ { define "whitelist" } } Белый список { { end } }
{ { define "telegram" } } Вход через Телеграм { { end } }
2022-04-09 08:28:57 +00:00
`
)
func InitHandlers ( r * mux . Router ) {
r . PathPrefix ( "/help" ) . HandlerFunc ( handlerHelp )
2022-05-18 16:58:24 +00:00
chain = viewutil . CopyEnRuWith ( fs , "view_help.html" , ruTranslation )
2022-04-09 08:28:57 +00:00
}
// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed.
func handlerHelp ( w http . ResponseWriter , rq * http . Request ) {
// See the history of this file to resurrect the old algorithm that supported multiple languages
var (
meta = viewutil . MetaFrom ( w , rq )
articlePath = strings . TrimPrefix ( strings . TrimPrefix ( rq . URL . Path , "/help/" ) , "/help" )
lang = "en"
)
if articlePath == "" {
articlePath = "en"
}
if ! strings . HasPrefix ( articlePath , "en" ) {
w . WriteHeader ( http . StatusNotFound )
_ , _ = io . WriteString ( w , "404 Not found" )
return
}
content , err := Get ( articlePath )
if err != nil && strings . HasPrefix ( err . Error ( ) , "open" ) {
w . WriteHeader ( http . StatusNotFound )
2022-04-23 18:53:15 +00:00
viewHelp ( meta , lang , "" , articlePath )
2022-04-09 08:28:57 +00:00
return
}
if err != nil {
w . WriteHeader ( http . StatusInternalServerError )
2022-04-23 18:53:15 +00:00
viewHelp ( meta , lang , err . Error ( ) , articlePath )
2022-04-09 08:28:57 +00:00
return
}
// TODO: change for the function that uses byte array when there is such function in mycomarkup.
ctx , _ := mycocontext . ContextFromStringInput ( string ( content ) , shroom . MarkupOptions ( articlePath ) )
ast := mycomarkup . BlockTree ( ctx )
result := mycomarkup . BlocksToHTML ( ctx , ast )
w . WriteHeader ( http . StatusOK )
2022-04-23 18:53:15 +00:00
viewHelp ( meta , lang , result , articlePath )
2022-04-09 08:28:57 +00:00
}
type helpData struct {
2022-05-18 16:46:01 +00:00
* viewutil . BaseData
2022-04-09 11:55:55 +00:00
ContentsHTML string
Lang string
2022-04-09 08:28:57 +00:00
}
2022-04-23 18:53:15 +00:00
func viewHelp ( meta viewutil . Meta , lang , contentsHTML , articlePath string ) {
2022-05-18 16:46:01 +00:00
viewutil . ExecutePage ( meta , chain , helpData {
BaseData : & viewutil . BaseData {
Addr : "/help/" + articlePath ,
2022-04-09 08:28:57 +00:00
} ,
2022-04-09 11:55:55 +00:00
ContentsHTML : contentsHTML ,
Lang : lang ,
2022-05-18 16:46:01 +00:00
} )
2022-04-09 08:28:57 +00:00
}