Hello World with children
+ ++ + Overview
Write some beautiful docs
+diff --git a/builder.go b/builder.go index e5b5e60..11e99a8 100644 --- a/builder.go +++ b/builder.go @@ -4,8 +4,14 @@ import ( "context" "embed" "fmt" + "io" + "io/fs" "net/http" + "net/http/httptest" + "os" + "path/filepath" "sort" + "strings" "time" "github.com/goplaid/web" @@ -23,6 +29,7 @@ type Builder struct { builder *web.Builder assets embed.FS assetsPrefix string + sitePrefix string } func New() (r *Builder) { @@ -43,6 +50,11 @@ func (b *Builder) Assets(prefix string, v embed.FS) (r *Builder) { return b } +func (b *Builder) SitePrefix(v string) (r *Builder) { + b.sitePrefix = v + return b +} + func (b *Builder) Header(v HTMLComponent) (r *Builder) { b.header = v return b @@ -77,14 +89,72 @@ func (b *Builder) Build() (r *Builder) { return b } +func (b *Builder) BuildStaticSite(dir string) { + dir = strings.NewReplacer(" ", "_").Replace(dir) + if len(dir) == 0 || dir == "/" { + dir = "dist" + } + + handler := b.Build() + err1 := os.RemoveAll(dir) + if err1 != nil { + fmt.Println("removing ", dir, err1) + } + + var paths = []string{ + "/index.css", + "/index.js", + } + + for _, m := range b.mounts { + paths = append(paths, m.path) + } + + fs.WalkDir(b.assets, ".", func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + return nil + } + paths = append(paths, filepath.Join("/", path)) + return nil + }) + + for _, p := range paths { + r := httptest.NewRequest("GET", p, nil) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + func() { + path := filepath.Join(dir, p) + fmt.Println("Generating ", path) + err := os.MkdirAll(filepath.Dir(path), 0755) + if err != nil { + panic(err) + } + var f *os.File + f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + panic(err) + } + defer f.Close() + + _, err = io.Copy(f, w.Body) + if err != nil { + panic(err) + } + }() + + } + +} + func (b *Builder) layout(body *DocBuilder) (r HTMLComponent) { return HTML( Head( Title(body.GetPageTitle()), Meta().Name("description").Content(body.abstractText), RawHTML(``), - Link("/index.css").Rel("stylesheet").Type("text/css"), - Script("").Attr("defer", true).Src("/index.js"), + Base().Href(b.sitePrefix), + Link("index.css").Rel("stylesheet").Type("text/css"), + Script("").Attr("defer", true).Src("index.js"), ), Body( Div( @@ -113,7 +183,7 @@ func (b *Builder) navigation(doc *DocBuilder) (r HTMLComponent) { arrowIcon, ).Class("w-3 m-2 flex fill-current text-gray-500"), ), - A().Href(items[i].URL).Text(items[i].Title). + A().Href(items[i].GetPageURL()).Text(items[i].Title). Class("text-gray-50"), ).Class("inline-flex"), ) @@ -161,7 +231,7 @@ func (s uriSorter) Less(i, j int) bool { func (b *Builder) addToMounts(node *DocNode) { // println(node.URL) b.mounts = append(b.mounts, - &mount{node.GetPageURL(), func(w http.ResponseWriter, r *http.Request) { + &mount{filepath.Join("/", node.GetPageURL()), func(w http.ResponseWriter, r *http.Request) { err := Fprint(w, b.layout(node.Doc), context.TODO()) if err != nil { panic(err) diff --git a/dev.sh b/dev.sh index 37dde22..e24cf92 100755 --- a/dev.sh +++ b/dev.sh @@ -1,13 +1,16 @@ DIR=$(PWD) snippetgo -pkg=docs > ./docs/examples-generated.go +cd $(PWD)/docs +go run ./build/main.go + function docsRestart() { echo "=================>" killall docgodocs # export DEV_CORE_JS=1 # export DEV_VUETIFY_JS=1 # export DEV_PRESETS=1 - go build -o /tmp/docgodocs docs/docsmain/main.go && /tmp/docgodocs + go build -o /tmp/docgodocs ./docsmain/main.go && /tmp/docgodocs } export -f docsRestart diff --git a/doc.go b/doc.go index 44eb4ec..d43171d 100644 --- a/doc.go +++ b/doc.go @@ -28,9 +28,9 @@ func (n *DocNode) AddChild(child *DocNode) { func (n *DocNode) GetPageURL() (r string) { if n.URL == "/" { - return "/index.html" + return "index.html" } - return fmt.Sprintf("%s.html", n.URL) + return strings.TrimLeft(fmt.Sprintf("%s.html", n.URL), "/") } type DocBuilder struct { diff --git a/docs/build/main.go b/docs/build/main.go new file mode 100644 index 0000000..1fba9eb --- /dev/null +++ b/docs/build/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/theplant/docgo" + "github.com/theplant/docgo/docs" +) + +func main() { + docgo.New(). + Assets("/assets/", docs.Assets). + Home(docs.Home). + SitePrefix("/docgo/"). + BuildStaticSite("dist") +} diff --git a/docs/dist/assets/logo.png b/docs/dist/assets/logo.png new file mode 100644 index 0000000..ad0bc14 Binary files /dev/null and b/docs/dist/assets/logo.png differ diff --git a/docs/dist/hello-world-with-children.html b/docs/dist/hello-world-with-children.html new file mode 100644 index 0000000..8853c08 --- /dev/null +++ b/docs/dist/hello-world-with-children.html @@ -0,0 +1,108 @@ + + + +
+Write some beautiful docs
+Bye, There
+Hi, There
+ +var Hello = true
+
Write some beautiful docs
+ +This is quite important to learn
+Doc of that section
+ +The developer community often use markdown or in Python world use Sphinx and reStructuredText to create tech documentations, We think they are nice, But because of these reasons we created this package for the Go community:
+ +The following code is used to build this doc +Hello World +
+ +Use the following code to boot up your doc app, Suppose you have already created a Home
Doc in docs package.
Use ChildrenTable(...)
to put other docs into current doc page children, The doc url will reflect the hierarchy, children docs url will contain parent doc slug
Since it's not possible to write
+`
+if you are writing inside a
+go raw string literals
+which normally used to pass in
+Markdown
+func, So we have to replace it with
+~
+