Skip to content

Commit

Permalink
Add generated static site
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed Nov 11, 2021
1 parent c7cf115 commit 5ef3967
Show file tree
Hide file tree
Showing 16 changed files with 982 additions and 8 deletions.
78 changes: 74 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,6 +29,7 @@ type Builder struct {
builder *web.Builder
assets embed.FS
assetsPrefix string
sitePrefix string
}

func New() (r *Builder) {
Expand All @@ -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
Expand Down Expand Up @@ -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(`<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">`),
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(
Expand Down Expand Up @@ -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"),
)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion dev.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions docs/build/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Binary file added docs/dist/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions docs/dist/hello-world-with-children.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<!DOCTYPE html>

<html>
<head>
<title>Hello World with children - docgo Documentation</title>

<meta name='description' content='Hello world with children is to describe how to add children docs'>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<base href='/docgo/'>

<link href='index.css' rel='stylesheet' type='text/css'>

<script type='text/javascript' defer src='index.js'></script>
</head>

<body>
<div id='app' v-cloak>
<nav class='bg-gray-700 py-3 text-base font-normal mb-8'>
<div aria-label='Breadcrumbs' class='flex list-none lg:max-w-5xl mx-auto px-10'>
<div class='inline-flex'>
<a href='index.html' class='text-gray-50'>docgo Documentation</a>
</div>

<div class='inline-flex'>
<div class='w-3 m-2 flex fill-current text-gray-500'>
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 14" data-v-134594fd="" data-v-838665fe=""><path data-v-7abeccde="" d="m4.81347656 13.1269531c.22558594 0 .41015625-.0820312.56738282-.2324219l5.31835942-5.19531245c.1845703-.19140625.2802734-.38964844.2802734-.63574219 0-.23925781-.0888672-.45117187-.2802734-.62890625l-5.31835942-5.20214843c-.15722657-.15039063-.34179688-.23242188-.56738282-.23242188-.45800781 0-.81347656.35546875-.81347656.80664062 0 .21875.09570312.43066407.24609375.58789063l4.79199219 4.67578125-4.79199219 4.6621094c-.15722656.1640625-.24609375.3623047-.24609375.5878906 0 .4511719.35546875.8066406.81347656.8066406z"></path></svg>
</div>

<a href='hello-world-with-children.html' class='text-gray-50'>Hello World with children</a>
</div>
</div>
</nav>

<div>
<main class='lg:max-w-5xl mx-auto px-10'>
<h1>Hello World with children</h1>

<div class='sm:flex mt-8 mb-16'>
<div class='sm:w-9/12'>
<div class='mb-8 text-xl font-normal'>Hello world with children is to describe how to add children docs</div>

<div class='border-t'><h2><a name="overview" class="anchor" href="#overview" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>Overview</h2>

<p>Write some beautiful docs</p>
</div>
</div>

<div class='ml-4 sm:w-3/12 font-medium text-base hidden sm:block text-gray-600'>
<div class='sticky top-4'>On This Page<toc></toc></div>
</div>
</div>
</main>

<div class='pt-4 pb-16 bg-gray-50'>
<section class='py-4'>
<div class='lg:max-w-5xl mx-auto px-10 mt-5'>
<h2>Topics</h2>

<div class='sm:flex sm:border-t mt-8'>
<div class='sm:w-1/4 pb-4 border-b sm:border-none'>
<h3>Essentials</h3>
</div>

<div class='sm:w-3/4'>
<div class='mt-8'>
<a href='hello-world-with-children/how-to-say-hello.html' class='inline-flex'>
<div class='w-4 flex mr-4 text-gray-500 fill-current'>
<svg aria-hidden="true" class=""
viewBox="0 0 15 15" xmlns="http://www.w3.org/2000/svg">
<path
d="m11.2623182 15c1.365556 0 2.055373-.7038949 2.055373-2.069451v-6.60957293c0-.76020648-.0985453-1.09103707-.5631159-1.5696856l-4.13890191-4.18113561c-.45049272-.45753168-.81651806-.57015486-1.4992961-.57015486h-3.37869545c-1.35147818 0-2.05537306.70389489-2.05537306 2.07648991v10.85405909c0 1.3725951.69685593 2.069451 2.05537306 2.069451zm-.0422337-.8657907h-7.44016897c-.80947912 0-1.2247771-.4293759-1.2247771-1.2247771v-10.81182544c0-.78132332.41529798-1.2247771 1.23181605-1.2247771h3.28718911v4.23744721c0 .80244016.40825904 1.1825434 1.18958236 1.1825434h4.18817455v6.61661193c0 .7954012-.4223369 1.2247771-1.231816 1.2247771zm1.0558423-8.66494604h-3.92069451c-.32379165 0-.45753168-.12670108-.45753168-.45753168v-3.9629282z"></path>
</svg>
</div>

<span>How to say hello</span>
</a>

<div class='ml-8'>
<div></div>
</div>
</div>

<div class='mt-8'>
<a href='hello-world-with-children/how-to-say-good-bye.html' class='inline-flex'>
<div class='w-4 flex mr-4 text-gray-500 fill-current'>
<svg aria-hidden="true" class=""
viewBox="0 0 15 15" xmlns="http://www.w3.org/2000/svg">
<path
d="m11.2623182 15c1.365556 0 2.055373-.7038949 2.055373-2.069451v-6.60957293c0-.76020648-.0985453-1.09103707-.5631159-1.5696856l-4.13890191-4.18113561c-.45049272-.45753168-.81651806-.57015486-1.4992961-.57015486h-3.37869545c-1.35147818 0-2.05537306.70389489-2.05537306 2.07648991v10.85405909c0 1.3725951.69685593 2.069451 2.05537306 2.069451zm-.0422337-.8657907h-7.44016897c-.80947912 0-1.2247771-.4293759-1.2247771-1.2247771v-10.81182544c0-.78132332.41529798-1.2247771 1.23181605-1.2247771h3.28718911v4.23744721c0 .80244016.40825904 1.1825434 1.18958236 1.1825434h4.18817455v6.61661193c0 .7954012-.4223369 1.2247771-1.231816 1.2247771zm1.0558423-8.66494604h-3.92069451c-.32379165 0-.45753168-.12670108-.45753168-.45753168v-3.9629282z"></path>
</svg>
</div>

<span>How to say good bye</span>
</a>

<div class='ml-8'>
<div></div>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</body>
</html>
62 changes: 62 additions & 0 deletions docs/dist/hello-world-with-children/how-to-say-good-bye.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>

<html>
<head>
<title>How to say good bye - Hello World with children - docgo Documentation</title>

<meta name='description'>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<base href='/docgo/'>

<link href='index.css' rel='stylesheet' type='text/css'>

<script type='text/javascript' defer src='index.js'></script>
</head>

<body>
<div id='app' v-cloak>
<nav class='bg-gray-700 py-3 text-base font-normal mb-8'>
<div aria-label='Breadcrumbs' class='flex list-none lg:max-w-5xl mx-auto px-10'>
<div class='inline-flex'>
<a href='index.html' class='text-gray-50'>docgo Documentation</a>
</div>

<div class='inline-flex'>
<div class='w-3 m-2 flex fill-current text-gray-500'>
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 14" data-v-134594fd="" data-v-838665fe=""><path data-v-7abeccde="" d="m4.81347656 13.1269531c.22558594 0 .41015625-.0820312.56738282-.2324219l5.31835942-5.19531245c.1845703-.19140625.2802734-.38964844.2802734-.63574219 0-.23925781-.0888672-.45117187-.2802734-.62890625l-5.31835942-5.20214843c-.15722657-.15039063-.34179688-.23242188-.56738282-.23242188-.45800781 0-.81347656.35546875-.81347656.80664062 0 .21875.09570312.43066407.24609375.58789063l4.79199219 4.67578125-4.79199219 4.6621094c-.15722656.1640625-.24609375.3623047-.24609375.5878906 0 .4511719.35546875.8066406.81347656.8066406z"></path></svg>
</div>

<a href='hello-world-with-children.html' class='text-gray-50'>Hello World with children</a>
</div>

<div class='inline-flex'>
<div class='w-3 m-2 flex fill-current text-gray-500'>
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 14" data-v-134594fd="" data-v-838665fe=""><path data-v-7abeccde="" d="m4.81347656 13.1269531c.22558594 0 .41015625-.0820312.56738282-.2324219l5.31835942-5.19531245c.1845703-.19140625.2802734-.38964844.2802734-.63574219 0-.23925781-.0888672-.45117187-.2802734-.62890625l-5.31835942-5.20214843c-.15722657-.15039063-.34179688-.23242188-.56738282-.23242188-.45800781 0-.81347656.35546875-.81347656.80664062 0 .21875.09570312.43066407.24609375.58789063l4.79199219 4.67578125-4.79199219 4.6621094c-.15722656.1640625-.24609375.3623047-.24609375.5878906 0 .4511719.35546875.8066406.81347656.8066406z"></path></svg>
</div>

<a href='hello-world-with-children/how-to-say-good-bye.html' class='text-gray-50'>How to say good bye</a>
</div>
</div>
</nav>

<div>
<main class='lg:max-w-5xl mx-auto px-10'>
<h1>How to say good bye</h1>

<div class='sm:flex mt-8 mb-16'>
<div class='sm:w-9/12'>
<div class='border-t'><h2><a name="say-good-bye" class="anchor" href="#say-good-bye" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>Say Good Bye</h2>

<p>Bye, There</p>
</div>
</div>

<div class='ml-4 sm:w-3/12 font-medium text-base hidden sm:block text-gray-600'>
<div class='sticky top-4'>On This Page<toc></toc></div>
</div>
</div>
</main>
</div>
</div>
</body>
</html>
Loading

0 comments on commit 5ef3967

Please sign in to comment.