2017-05-29 14:52:37 +00:00
/* Copyright Azareal 2016 - 2018 */
2017-11-10 03:33:11 +00:00
package common
2017-01-01 15:45:43 +00:00
2017-05-29 14:52:37 +00:00
import (
//"fmt"
2017-06-25 09:56:39 +00:00
"bytes"
2017-11-11 23:34:27 +00:00
"database/sql"
2017-09-03 04:50:31 +00:00
"encoding/json"
2017-08-13 11:22:34 +00:00
"errors"
2018-04-22 12:33:56 +00:00
"io"
2017-05-29 14:52:37 +00:00
"io/ioutil"
2017-09-03 04:50:31 +00:00
"log"
"mime"
2017-05-29 14:52:37 +00:00
"net/http"
2017-09-03 04:50:31 +00:00
"os"
"path/filepath"
"reflect"
"strings"
2017-09-10 16:57:22 +00:00
"sync"
"sync/atomic"
2017-06-25 09:56:39 +00:00
"text/template"
2017-11-11 04:06:16 +00:00
"../query_gen/lib"
2017-05-29 14:52:37 +00:00
)
2017-01-01 15:45:43 +00:00
2017-12-01 02:04:29 +00:00
type ThemeList map [ string ] * Theme
2017-11-06 04:02:35 +00:00
2017-12-01 02:04:29 +00:00
var Themes ThemeList = make ( map [ string ] * Theme )
2017-11-10 03:33:11 +00:00
var DefaultThemeBox atomic . Value
2017-11-11 04:06:16 +00:00
var ChangeDefaultThemeMutex sync . Mutex
2017-01-01 15:45:43 +00:00
2017-09-22 02:21:17 +00:00
// TODO: Use this when the default theme doesn't exist
2018-03-17 08:16:43 +00:00
var fallbackTheme = "cosora"
2017-09-03 04:50:31 +00:00
var overridenTemplates = make ( map [ string ] bool )
type Theme struct {
2017-11-29 02:34:02 +00:00
Name string
FriendlyName string
Version string
Creator string
FullImage string
MobileFriendly bool
Disabled bool
HideFromThemes bool
2017-12-22 03:32:23 +00:00
BgAvatars bool // For profiles, at the moment
2017-11-29 02:34:02 +00:00
ForkOf string
Tag string
URL string
Docks [ ] string // Allowed Values: leftSidebar, rightSidebar, footer
2017-09-03 04:50:31 +00:00
Settings map [ string ] ThemeSetting
Templates [ ] TemplateMapping
TemplatesMap map [ string ] string
TmplPtr map [ string ] interface { }
Resources [ ] ThemeResource
2017-06-25 09:56:39 +00:00
ResourceTemplates * template . Template
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
// This variable should only be set and unset by the system, not the theme meta file
Active bool
}
2017-09-03 04:50:31 +00:00
type ThemeSetting struct {
2017-01-01 15:45:43 +00:00
FriendlyName string
2017-09-03 04:50:31 +00:00
Options [ ] string
2017-01-01 15:45:43 +00:00
}
2017-09-03 04:50:31 +00:00
type TemplateMapping struct {
Name string
2017-01-01 15:45:43 +00:00
Source string
//When string
}
2017-09-03 04:50:31 +00:00
type ThemeResource struct {
Name string
2017-06-19 08:06:54 +00:00
Location string
2018-03-21 05:56:33 +00:00
Loggedin bool // Only serve this resource to logged in users
2017-06-19 08:06:54 +00:00
}
2017-11-11 23:34:27 +00:00
type ThemeStmts struct {
getThemes * sql . Stmt
}
var themeStmts ThemeStmts
2017-09-23 19:57:13 +00:00
func init ( ) {
2017-11-11 04:06:16 +00:00
DefaultThemeBox . Store ( fallbackTheme )
2017-11-12 03:29:05 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2017-11-11 23:34:27 +00:00
themeStmts = ThemeStmts {
getThemes : acc . Select ( "themes" ) . Columns ( "uname, default" ) . Prepare ( ) ,
}
return acc . FirstError ( )
} )
2017-09-23 19:57:13 +00:00
}
2017-11-06 04:02:35 +00:00
// TODO: Make the initThemes and LoadThemes functions less confusing
2017-09-23 19:57:13 +00:00
// ? - Delete themes which no longer exist in the themes folder from the database?
2017-11-11 04:06:16 +00:00
func ( themes ThemeList ) LoadActiveStatus ( ) error {
ChangeDefaultThemeMutex . Lock ( )
2017-11-11 23:34:27 +00:00
rows , err := themeStmts . getThemes . Query ( )
2017-06-14 09:55:47 +00:00
if err != nil {
return err
}
defer rows . Close ( )
var uname string
var defaultThemeSwitch bool
for rows . Next ( ) {
err = rows . Scan ( & uname , & defaultThemeSwitch )
if err != nil {
return err
}
// Was the theme deleted at some point?
theme , ok := themes [ uname ]
if ! ok {
continue
}
if defaultThemeSwitch {
2017-11-29 02:34:02 +00:00
log . Printf ( "Loading the default theme '%s'" , theme . Name )
2017-06-14 09:55:47 +00:00
theme . Active = true
2017-11-11 04:06:16 +00:00
DefaultThemeBox . Store ( theme . Name )
2017-12-01 02:04:29 +00:00
theme . MapTemplates ( )
2017-06-14 09:55:47 +00:00
} else {
2017-11-29 02:34:02 +00:00
log . Printf ( "Loading the theme '%s'" , theme . Name )
2017-06-14 09:55:47 +00:00
theme . Active = false
}
2017-09-03 04:50:31 +00:00
2017-06-14 09:55:47 +00:00
themes [ uname ] = theme
}
2017-11-11 04:06:16 +00:00
ChangeDefaultThemeMutex . Unlock ( )
2017-09-03 04:50:31 +00:00
return rows . Err ( )
2017-06-14 09:55:47 +00:00
}
2018-03-11 09:33:49 +00:00
func ( themes ThemeList ) LoadStaticFiles ( ) error {
for _ , theme := range themes {
err := theme . LoadStaticFiles ( )
if err != nil {
return err
}
}
return nil
}
2017-11-11 04:06:16 +00:00
func InitThemes ( ) error {
2017-01-01 15:45:43 +00:00
themeFiles , err := ioutil . ReadDir ( "./themes" )
if err != nil {
2017-09-03 04:50:31 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
for _ , themeFile := range themeFiles {
if ! themeFile . IsDir ( ) {
continue
}
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
themeName := themeFile . Name ( )
2017-11-29 02:34:02 +00:00
log . Printf ( "Adding theme '%s'" , themeName )
2017-01-01 15:45:43 +00:00
themeFile , err := ioutil . ReadFile ( "./themes/" + themeName + "/theme.json" )
if err != nil {
2017-09-03 04:50:31 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2017-12-01 02:04:29 +00:00
var theme = & Theme { Name : "" }
err = json . Unmarshal ( themeFile , theme )
2017-01-07 06:31:04 +00:00
if err != nil {
2017-09-03 04:50:31 +00:00
return err
2017-01-07 06:31:04 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
theme . Active = false // Set this to false, just in case someone explicitly overrode this value in the JSON file
2017-05-29 14:52:37 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// TODO: Let the theme specify where it's resources are via the JSON file?
// TODO: Let the theme inherit CSS from another theme?
// ? - This might not be too helpful, as it only searches for /public/ and not if /public/ is empty. Still, it might help some people with a slightly less cryptic error
_ , err = os . Stat ( "./themes/" + theme . Name + "/public/" )
if err != nil {
if os . IsNotExist ( err ) {
return errors . New ( "We couldn't find this theme's resources. E.g. the /public/ folder." )
} else {
log . Print ( "We weren't able to access this theme's resources due to a permissions issue or some other problem" )
return err
}
}
2017-01-07 06:31:04 +00:00
if theme . FullImage != "" {
2018-02-19 04:26:01 +00:00
DebugLog ( "Adding theme image" )
2017-11-11 04:06:16 +00:00
err = StaticFiles . Add ( "./themes/" + themeName + "/" + theme . FullImage , "./themes/" + themeName )
2017-01-07 06:31:04 +00:00
if err != nil {
2017-09-03 04:50:31 +00:00
return err
2017-01-07 06:31:04 +00:00
}
}
2017-05-29 14:52:37 +00:00
2017-09-23 19:57:13 +00:00
theme . TemplatesMap = make ( map [ string ] string )
theme . TmplPtr = make ( map [ string ] interface { } )
if theme . Templates != nil {
for _ , themeTmpl := range theme . Templates {
theme . TemplatesMap [ themeTmpl . Name ] = themeTmpl . Source
2017-11-11 04:06:16 +00:00
theme . TmplPtr [ themeTmpl . Name ] = TmplPtrMap [ "o_" + themeTmpl . Source ]
2017-09-23 19:57:13 +00:00
}
}
2017-11-11 04:06:16 +00:00
Themes [ theme . Name ] = theme
2017-01-01 15:45:43 +00:00
}
2017-09-03 04:50:31 +00:00
return nil
2017-01-01 15:45:43 +00:00
}
2017-12-01 02:04:29 +00:00
// TODO: It might be unsafe to call the template parsing functions with fsnotify, do something more concurrent
func ( theme * Theme ) LoadStaticFiles ( ) error {
theme . ResourceTemplates = template . New ( "" )
template . Must ( theme . ResourceTemplates . ParseGlob ( "./themes/" + theme . Name + "/public/*.css" ) )
// It should be safe for us to load the files for all the themes in memory, as-long as the admin hasn't setup a ridiculous number of themes
return theme . AddThemeStaticFiles ( )
}
func ( theme * Theme ) AddThemeStaticFiles ( ) error {
2018-03-11 09:33:49 +00:00
phraseMap := GetCSSPhrases ( )
2017-09-10 16:57:22 +00:00
// TODO: Use a function instead of a closure to make this more testable? What about a function call inside the closure to take the theme variable into account?
2017-09-03 04:50:31 +00:00
return filepath . Walk ( "./themes/" + theme . Name + "/public" , func ( path string , f os . FileInfo , err error ) error {
2018-02-19 04:26:01 +00:00
DebugLog ( "Attempting to add static file '" + path + "' for default theme '" + theme . Name + "'" )
2017-01-01 15:45:43 +00:00
if err != nil {
return err
}
if f . IsDir ( ) {
return nil
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
path = strings . Replace ( path , "\\" , "/" , - 1 )
2017-01-01 15:45:43 +00:00
data , err := ioutil . ReadFile ( path )
if err != nil {
return err
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
var ext = filepath . Ext ( path )
2017-07-17 10:23:42 +00:00
if ext == ".css" && len ( data ) != 0 {
2017-06-25 09:56:39 +00:00
var b bytes . Buffer
2017-09-03 04:50:31 +00:00
var pieces = strings . Split ( path , "/" )
var filename = pieces [ len ( pieces ) - 1 ]
2018-03-11 09:33:49 +00:00
err = theme . ResourceTemplates . ExecuteTemplate ( & b , filename , CSSData { Phrases : phraseMap } )
2017-06-25 09:56:39 +00:00
if err != nil {
return err
}
data = b . Bytes ( )
2017-05-07 08:31:41 +00:00
}
2017-06-25 09:56:39 +00:00
2017-09-03 04:50:31 +00:00
path = strings . TrimPrefix ( path , "themes/" + theme . Name + "/public" )
gzipData := compressBytesGzip ( data )
2017-12-01 02:04:29 +00:00
StaticFiles . Set ( "/static/" + theme . Name + path , SFile { data , gzipData , 0 , int64 ( len ( data ) ) , int64 ( len ( gzipData ) ) , mime . TypeByExtension ( ext ) , f , f . ModTime ( ) . UTC ( ) . Format ( http . TimeFormat ) } )
2017-05-29 14:52:37 +00:00
2018-02-19 04:26:01 +00:00
DebugLog ( "Added the '/" + theme . Name + path + "' static file for theme " + theme . Name + "." )
2017-01-01 15:45:43 +00:00
return nil
} )
}
2017-12-01 02:04:29 +00:00
func ( theme * Theme ) MapTemplates ( ) {
2017-01-01 15:45:43 +00:00
if theme . Templates != nil {
for _ , themeTmpl := range theme . Templates {
if themeTmpl . Name == "" {
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "Invalid destination template name" ) )
2017-01-01 15:45:43 +00:00
}
if themeTmpl . Source == "" {
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "Invalid source template name" ) )
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-07 06:31:04 +00:00
// `go generate` is one possibility for letting plugins inject custom page structs, but it would simply add another step of compilation. It might be simpler than the current build process from the perspective of the administrator?
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
destTmplPtr , ok := TmplPtrMap [ themeTmpl . Name ]
2017-01-01 15:45:43 +00:00
if ! ok {
2017-06-16 10:41:30 +00:00
return
2017-01-01 15:45:43 +00:00
}
2017-11-11 04:06:16 +00:00
sourceTmplPtr , ok := TmplPtrMap [ themeTmpl . Source ]
2017-01-01 15:45:43 +00:00
if ! ok {
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source template doesn't exist!" ) )
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
switch dTmplPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( TopicListPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicListPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( ForumPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ForumPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( ForumsPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ForumsPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( ProfilePage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ProfilePage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
2017-01-01 15:45:43 +00:00
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( CreateTopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( CreateTopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2018-03-08 03:59:47 +00:00
}
2018-04-22 12:33:56 +00:00
case * func ( Page , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch sTmplPtr := sourceTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( Page , io . Writer ) error :
2017-09-03 04:50:31 +00:00
//overridenTemplates[themeTmpl.Name] = d_tmpl_ptr
overridenTemplates [ themeTmpl . Name ] = true
* dTmplPtr = * sTmplPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
default :
2018-03-17 08:16:43 +00:00
log . Print ( "themeTmpl.Name: " , themeTmpl . Name )
log . Print ( "themeTmpl.Source: " , themeTmpl . Source )
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "Unknown destination template type!" ) )
2017-01-01 15:45:43 +00:00
}
}
}
}
2017-11-11 04:06:16 +00:00
func ResetTemplateOverrides ( ) {
2017-01-01 15:45:43 +00:00
log . Print ( "Resetting the template overrides" )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
for name := range overridenTemplates {
2017-01-01 15:45:43 +00:00
log . Print ( "Resetting '" + name + "' template override" )
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
originPointer , ok := TmplPtrMap [ "o_" + name ]
2017-01-01 15:45:43 +00:00
if ! ok {
log . Print ( "The origin template doesn't exist!" )
return
}
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
destTmplPtr , ok := TmplPtrMap [ name ]
2017-01-01 15:45:43 +00:00
if ! ok {
log . Print ( "The destination template doesn't exist!" )
return
}
2017-05-29 14:52:37 +00:00
2017-01-01 16:14:38 +00:00
// Not really a pointer, more of a function handle, an artifact from one of the earlier versions of themes.go
2017-09-03 04:50:31 +00:00
switch oPtr := originPointer . ( type ) {
2018-04-22 12:33:56 +00:00
case func ( TopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
2017-01-01 15:45:43 +00:00
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( TopicListPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicListPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( ForumPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ForumPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( ForumsPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ForumsPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( ProfilePage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( ProfilePage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( CreateTopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( CreateTopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2018-03-08 03:59:47 +00:00
}
2018-04-22 12:33:56 +00:00
case func ( Page , io . Writer ) error :
2017-09-03 04:50:31 +00:00
switch dPtr := destTmplPtr . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( Page , io . Writer ) error :
2017-09-03 04:50:31 +00:00
* dPtr = oPtr
default :
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
2017-09-03 04:50:31 +00:00
}
default :
2018-03-17 08:16:43 +00:00
log . Print ( "name: " , name )
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "Unknown destination template type!" ) )
2017-01-01 15:45:43 +00:00
}
log . Print ( "The template override was reset" )
}
2017-09-03 04:50:31 +00:00
overridenTemplates = make ( map [ string ] bool )
2017-01-01 15:45:43 +00:00
log . Print ( "All of the template overrides have been reset" )
}
2017-08-13 11:22:34 +00:00
// NEW method of doing theme templates to allow one user to have a different theme to another. Under construction.
2017-09-10 16:57:22 +00:00
// TODO: Generate the type switch instead of writing it by hand
// TODO: Cut the number of types in half
2018-04-22 12:33:56 +00:00
func RunThemeTemplate ( theme string , template string , pi interface { } , w io . Writer ) error {
2017-10-30 09:57:08 +00:00
var getTmpl = GetThemeTemplate ( theme , template )
switch tmplO := getTmpl . ( type ) {
2018-04-22 12:33:56 +00:00
case * func ( TopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( TopicPage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( TopicListPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2018-04-22 12:33:56 +00:00
return tmpl ( pi . ( TopicListPage ) , w )
case * func ( ForumPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( ForumPage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( ForumsPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( ForumsPage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( ProfilePage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( ProfilePage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( CreateTopicPage , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( CreateTopicPage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
var tmpl = * tmplO
return tmpl ( pi . ( IPSearchPage ) , w )
2018-04-22 12:33:56 +00:00
case * func ( Page , io . Writer ) error :
2017-09-03 04:50:31 +00:00
var tmpl = * tmplO
2017-10-30 09:57:08 +00:00
return tmpl ( pi . ( Page ) , w )
2018-04-22 12:33:56 +00:00
case func ( TopicPage , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( TopicPage ) , w )
2018-04-22 12:33:56 +00:00
case func ( TopicListPage , io . Writer ) error :
return tmplO ( pi . ( TopicListPage ) , w )
case func ( ForumPage , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( ForumPage ) , w )
2018-04-22 12:33:56 +00:00
case func ( ForumsPage , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( ForumsPage ) , w )
2018-04-22 12:33:56 +00:00
case func ( ProfilePage , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( ProfilePage ) , w )
2018-04-22 12:33:56 +00:00
case func ( CreateTopicPage , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( CreateTopicPage ) , w )
2018-04-22 12:33:56 +00:00
case func ( IPSearchPage , io . Writer ) error :
2018-03-08 03:59:47 +00:00
return tmplO ( pi . ( IPSearchPage ) , w )
2018-04-22 12:33:56 +00:00
case func ( Page , io . Writer ) error :
2017-10-30 09:57:08 +00:00
return tmplO ( pi . ( Page ) , w )
2017-09-03 04:50:31 +00:00
case string :
2017-11-11 04:06:16 +00:00
mapping , ok := Themes [ DefaultThemeBox . Load ( ) . ( string ) ] . TemplatesMap [ template ]
2017-09-03 04:50:31 +00:00
if ! ok {
mapping = template
}
2017-11-11 04:06:16 +00:00
return Templates . ExecuteTemplate ( w , mapping + ".html" , pi )
2017-09-03 04:50:31 +00:00
default :
log . Print ( "theme " , theme )
log . Print ( "template " , template )
log . Print ( "pi " , pi )
log . Print ( "tmplO " , tmplO )
2017-10-30 09:57:08 +00:00
log . Print ( "getTmpl " , getTmpl )
2017-09-03 04:50:31 +00:00
valueOf := reflect . ValueOf ( tmplO )
log . Print ( "initial valueOf.Type()" , valueOf . Type ( ) )
for valueOf . Kind ( ) == reflect . Interface || valueOf . Kind ( ) == reflect . Ptr {
valueOf = valueOf . Elem ( )
log . Print ( "valueOf.Elem().Type() " , valueOf . Type ( ) )
}
log . Print ( "deferenced valueOf.Type() " , valueOf . Type ( ) )
log . Print ( "valueOf.Kind() " , valueOf . Kind ( ) )
2017-10-30 09:57:08 +00:00
return errors . New ( "Unknown template type" )
2017-08-13 11:22:34 +00:00
}
}
2017-09-03 04:50:31 +00:00
// GetThemeTemplate attempts to get the template for a specific theme, otherwise it falls back on the default template pointer, which if absent will fallback onto the template interpreter
2017-08-13 11:22:34 +00:00
func GetThemeTemplate ( theme string , template string ) interface { } {
2017-11-11 04:06:16 +00:00
tmpl , ok := Themes [ theme ] . TmplPtr [ template ]
2017-09-03 04:50:31 +00:00
if ok {
2017-08-13 11:22:34 +00:00
return tmpl
}
2017-11-11 04:06:16 +00:00
tmpl , ok = TmplPtrMap [ template ]
2017-09-03 04:50:31 +00:00
if ok {
return tmpl
}
return template
2017-08-13 11:22:34 +00:00
}
2017-09-03 04:50:31 +00:00
// CreateThemeTemplate creates a theme template on the current default theme
2017-08-13 11:22:34 +00:00
func CreateThemeTemplate ( theme string , name string ) {
2017-11-11 04:06:16 +00:00
Themes [ theme ] . TmplPtr [ name ] = func ( pi Page , w http . ResponseWriter ) error {
mapping , ok := Themes [ DefaultThemeBox . Load ( ) . ( string ) ] . TemplatesMap [ name ]
2017-08-13 11:22:34 +00:00
if ! ok {
mapping = name
}
2017-11-11 04:06:16 +00:00
return Templates . ExecuteTemplate ( w , mapping + ".html" , pi )
2017-08-13 11:22:34 +00:00
}
}
2017-09-10 16:57:22 +00:00
func GetDefaultThemeName ( ) string {
2017-11-11 04:06:16 +00:00
return DefaultThemeBox . Load ( ) . ( string )
2017-09-10 16:57:22 +00:00
}
func SetDefaultThemeName ( name string ) {
2017-11-11 04:06:16 +00:00
DefaultThemeBox . Store ( name )
2017-09-10 16:57:22 +00:00
}
2017-11-29 02:34:02 +00:00
func ( theme Theme ) HasDock ( name string ) bool {
for _ , dock := range theme . Docks {
if dock == name {
return true
}
}
return false
}
2018-03-17 08:16:43 +00:00
// TODO: Implement this
func ( theme Theme ) BuildDock ( dock string ) ( sbody string ) {
return ""
}