mango

package module
Version: v0.0.0-...-6ad051f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 22 Imported by: 0

README

Mango Go Report Card Code coverage Docs License

Serves markdown content as webpage.

Content structure

You can see content structure example in test-files/.
Or find runnable example in folder example/.

main.go
.mango
content/
    en/
        top-menu/
            1_Home.md
            2_About.md
            ...
        footer-menu/
            ...
    lv/
        ...
public/
    favicon.png
    images/
    css/
    js/

.mango - config file

Domain: https://example.loc
ContentPath: content/
PublicPath: public/

PageURL: /{Lang}/{Slug}.html
FileURL: /{File}

Examples

Check out example/README

Example (simple)

First install with go get bitbucket.org/briiC/mango-v3
and use in code as import "bitbucket.org/briiC/mango-v3"

One-liner if you need basic webpage functionality.

#!go
package main

import "bitbucket.org/briiC/mango-v3"

func main() {
    mango.NewServer(3000).Start()
}

Example (advanced)

Before starting webserver add custom stuff if you need advanced configuration.

#!go
package main

import "bitbucket.org/briiC/mango-v3"

func main() {
    srv := mango.NewServer(3000)

	// Add some middlewares ("File", "Page")
    // srv.Middlewares["Page"] = mwForPage  // assign one mw
    // srv.Middlewares["File"] = mwForFile  // assign one mw
	srv.Middlewares["Page"] = func(next http.Handler) http.Handler {
		return mwFirst(mwSecond(next))
	}

	// Custom functions for templates
	srv.FuncMap = template.FuncMap{
		"Smile": func() string {
			return ":)"
		},
		"Add": func(a,b int) string {
			return a + b
		},
	}

    // Custom route
	srv.Router.HandleFunc("/{Lang}/search/{sterm}", func(w http.ResponseWriter, r *http.Request) {
		runSearch(srv, w, r) // create your handler
	})

    // Print all pages and info about them
	srv.App.Print()

    // Go!
	log.Println("Start listening on", ":"+srv.Port)
	panic( srv.Start() )
}

func mwFirst(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// do stuff
		next.ServeHTTP(w, r)
	})
}
func mwSecond(next http.Handler) http.Handler { ... }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileToParams

func FileToParams(fpath string) map[string]string

FileToParams - Read file contents and convert to map of params We do not check for error because we only want to get/or not file Importance order: 1. Content params 2. Filename params 3. (same depth) .defaults 4 -n. (up n depth) .subdefaults 5. .mango config file params

func T

func T(page *Page, s string) string

T - Translate string to given language

func ToASCII

func ToASCII(str string) string

ToASCII - UTF to Ascii characters

func ToTime

func ToTime(s string) (time.Time, error)

ToTime - datetime string to time type Parse any custom string 2006-01-02 15:04:05 01 - month 02 - day

Types

type Application

type Application struct {

	// Absoluted domain path with scheme
	// https://example.loc
	Domain string

	// Absolute path to content (folders with .md files)
	ContentPath string

	// Absolute path to web accessible files
	PublicPath string

	// Page tree
	Pages PageList

	// URLTemplates - url templates for pages
	URLTemplates map[string]string

	// Link to server
	Server *Server

	// Run this func on content reload
	OnReload func(app *Application)
	// contains filtered or unexported fields
}

Application - mango application

func NewApplication

func NewApplication() (*Application, error)

NewApplication - create/init new application Must be executed only one time

func (*Application) BinPath

func (app *Application) BinPath() string

BinPath - get bin path

func (*Application) Collection

func (app *Application) Collection(ckey string) *Collection

Collection - get collection by ckey

func (*Application) CollectionCount

func (app *Application) CollectionCount() int

CollectionCount - total count of collections

func (*Application) CollectionPages

func (app *Application) CollectionPages(ckey, csubkey string) PageList

CollectionPages - shorthand to get collection subitems Avoiding errors. Without it need to use: app.Collection(ckey).Get(csubkey) But there could be no ckey collection and that results in go-lang error

func (*Application) FileToPage

func (app *Application) FileToPage(fpath string) *Page

FileToPage for application

func (*Application) IsValidLang

func (app *Application) IsValidLang(lang string) bool

IsValidLang - is given language is valid in App scope

func (*Application) LoadContent

func (app *Application) LoadContent()

LoadContent - Load files to application Can be executed more than once times (on .reload file creation)

func (*Application) NewPage

func (app *Application) NewPage(lang, label string) *Page

NewPage already linked to app

func (*Application) Page

func (app *Application) Page(slug string) *Page

Page - get one page by given slug. Slug must be equal and is case-sensitive

func (*Application) PageCount

func (app *Application) PageCount() int

PageCount - total count of pages

func (*Application) Print

func (app *Application) Print()

Print - output app highlights

func (*Application) Search

func (app *Application) Search(pageSlug, sterm string) PageList

Search pages from given top page

type Collection

type Collection struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Collection is map of *Page

func NewCollection

func NewCollection() *Collection

NewCollection - create and init as empty

func (*Collection) Append

func (c *Collection) Append(key string, page *Page)

Append new page to PageList under key

func (*Collection) Get

func (c *Collection) Get(key string) PageList

Get from local map by key

func (*Collection) Len

func (c *Collection) Len() int

Len is part of sort.Interface.

func (*Collection) MakeEmpty

func (c *Collection) MakeEmpty()

MakeEmpty - init or clear map

func (*Collection) Print

func (c *Collection) Print(label string)

Print collection items

func (*Collection) Remove

func (c *Collection) Remove(key string)

Remove by key

type Page

type Page struct {
	sync.RWMutex

	// Link to application
	App *Application

	// Parent page
	Parent *Page

	// Sub-pages for this page
	Pages PageList
	// contains filtered or unexported fields
}

Page - page with content and params + sub-pages

func (*Page) AbsoluteURL

func (page *Page) AbsoluteURL() string

AbsoluteURL - page URL prefixed with domain

func (*Page) AppendContent

func (page *Page) AppendContent(content []byte)

AppendContent - append to content

func (*Page) BlankParams

func (page *Page) BlankParams()

BlankParams - init/clear all params Or could be used to init page params map

func (*Page) Content

func (page *Page) Content() []byte

Content - get content for page

func (*Page) Get

func (page *Page) Get(key string) string

Get - get thread-safely param to Page.Params

func (*Page) IsDir

func (page *Page) IsDir() bool

IsDir - shorthand to find out is this val set and not empty "IsDir"

func (*Page) IsEqual

func (page *Page) IsEqual(key, val string) bool

IsEqual - shorthand to compare param with custom string

func (*Page) IsNegation

func (page *Page) IsNegation(key string) bool

IsNegation - Similar to IsNo, bu check for more negation forms Negation values are: No Not None N/A 0 -1 "" (empty, not set)

func (*Page) IsNo

func (page *Page) IsNo(key string) bool

IsNo - shorthand to compare param with "No"

func (*Page) IsSet

func (page *Page) IsSet(key string) bool

IsSet - shorthand to find out is this val set and not empty ""

func (*Page) IsYes

func (page *Page) IsYes(key string) bool

IsYes - shorthand to compare param with "Yes"

func (*Page) MergeParams

func (page *Page) MergeParams(moreParams map[string]string)

MergeParams - merge some more params

func (*Page) ModTime

func (page *Page) ModTime() time.Time

ModTime - Get mod time for page from params not reading actual file from filesystem

func (*Page) Paging

func (page *Page) Paging(pNum, pSize, pLimit int)

Paging - slice page.Pages with paging logic and add params from paging to page params

func (*Page) Params

func (page *Page) Params() map[string]string

Params - return map safaly

func (*Page) ParamsLen

func (page *Page) ParamsLen() int

ParamsLen - how many params page have

func (*Page) PopulateParams

func (page *Page) PopulateParams(s string) string

PopulateParams - replace given string with templated params Use figure brackets "{}" as param placeholders /{Slug}.html with be replaced with actual page slug

func (*Page) Print

func (page *Page) Print()

Print pages in list

func (*Page) PrintRow

func (page *Page) PrintRow()

PrintRow - print page as one row

func (*Page) PrintTree

func (page *Page) PrintTree(depth int)

PrintTree - Print all pages under this page

func (*Page) ReloadContent

func (page *Page) ReloadContent() bool

ReloadContent file Content (only) TODO: reload params too? changing Slug is dangerous

func (*Page) RemoveParam

func (page *Page) RemoveParam(key string)

RemoveParam - remove param by given key

func (*Page) Search

func (page *Page) Search(sterm string) PageList

Search - find all pages by given search term TODO: make correct search by params and content

func (*Page) SearchByParam

func (page *Page) SearchByParam(key, val string) PageList

SearchByParam - find all pages that search value is equal to page param

func (*Page) Set

func (page *Page) Set(key, val string)

Set - set thread-safely param to Page.Params

func (*Page) SetContent

func (page *Page) SetContent(content []byte)

SetContent set content for page

func (*Page) SetLang

func (page *Page) SetLang(lang string) string

SetLang - try to set page language If given lang is not valid, use default lang from App

func (*Page) SetValue

func (page *Page) SetValue(key string, val interface{})

SetValue - set any type value Interface variable will cast to string

func (*Page) Split

func (page *Page) Split(key, sep string) []string

Split - get param as slice splitted by given separator

func (*Page) Walk

func (page *Page) Walk(fnCheck func(p *Page) bool) PageList

Walk all down by sub-pages and do custom stuff Can be customized by custom func TODO: goroutines?

func (*Page) WalkTop

func (page *Page) WalkTop(fn func(parent *Page))

WalkTop - from current page to all parents on top

type PageList

type PageList []*Page

PageList is slice as []*Page

func (PageList) Len

func (pages PageList) Len() int

Len is part of sort.Interface.

func (PageList) Less

func (pages PageList) Less(i, j int) bool

Less is part of sort.Interface. We use count as the value to sort by

func (PageList) Paging

func (pages PageList) Paging(pNum, pSize, pLimit int) (PageList, map[string]int)

Paging - Make paging params Returns necessary params for paging

func (PageList) Print

func (pages PageList) Print()

Print pages in list

func (PageList) Randomize

func (pages PageList) Randomize()

Randomize slice Randomizes param SortNr for all pages and sorts

func (PageList) Sort

func (pages PageList) Sort(sortType string)

Sort list by given sortType

func (PageList) Swap

func (pages PageList) Swap(i, j int)

Swap is part of sort.Interface.

type PageMap

type PageMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

PageMap is map of *Page

func NewPageMap

func NewPageMap() *PageMap

NewPageMap - create and init as empty

func (*PageMap) Add

func (pm *PageMap) Add(key string, page *Page)

Add new *Page to local map Can't use only Slug, because PageMap can be used for many purposes

func (*PageMap) Filter

func (pm *PageMap) Filter(fnCheck func(p *Page) bool) PageList

Filter - filter by custom func

func (*PageMap) Get

func (pm *PageMap) Get(key string) *Page

Get from local map by key

func (*PageMap) Len

func (pm *PageMap) Len() int

Len - map item count

func (*PageMap) MakeEmpty

func (pm *PageMap) MakeEmpty()

MakeEmpty - init or clear map

func (*PageMap) Print

func (pm *PageMap) Print()

Print pages in list

func (*PageMap) Remove

func (pm *PageMap) Remove(key string)

Remove by key

type Server

type Server struct {
	Host      string
	Port      string
	App       *Application
	Templates *template.Template
	Router    *mux.Router
	FuncMap   template.FuncMap // user can define its

	/* Middlewares:
	Middlewares["Page"]
	Middlewares["File"]

	To add multiple middlewares on one map key:
	srv.Middlewares["File"] = func(next http.Handler) http.Handler {
		return mwFirst(mwSecond(next))
	}
	*/
	Middlewares map[string]func(next http.Handler) http.Handler
}

Server - mango server for serving content from mango-structure data

func NewServer

func NewServer(port int) *Server

NewServer - create server instance

func (*Server) Render

func (srv *Server) Render(w io.Writer, page *Page, templateID string)

Render only layout But give param for page to distinct template

func (*Server) Run404

func (srv *Server) Run404(w http.ResponseWriter, r *http.Request)

Run404 - handler 404

func (*Server) RunIndex

func (srv *Server) RunIndex(w http.ResponseWriter, r *http.Request)

RunIndex - handler for first page

func (*Server) RunOne

func (srv *Server) RunOne(w http.ResponseWriter, r *http.Request)

RunOne - handler for specific one (*Page)

func (*Server) Start

func (srv *Server) Start() error

Start listening to port (default) Can't be tested because using httptest package (it have his own listener)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL