Skip to content

Commit

Permalink
Added working command example.
Browse files Browse the repository at this point in the history
- Page references no longer include the path or scheme.
  • Loading branch information
dsoprea committed Jan 7, 2019
1 parent 00ae89f commit 7c26ba5
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 41 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This project was created in order to solve the problem of producing an HTML-base

# Example

Example from [MarkdownDialect.RenderHtml](https://godoc.org/github.com/dsoprea/go-static-site-builder/markdown#example-MarkdownDialect-RenderHtml):
Aside from a functioning command example at [example/helloworld](https://godoc.org/github.com/dsoprea/go-static-site-builder/example/helloworld), this is from the example at [MarkdownDialect.RenderHtml](https://godoc.org/github.com/dsoprea/go-static-site-builder/markdown#example-MarkdownDialect-RenderHtml):

```go
tempPath, err := ioutil.TempDir("", "")
Expand All @@ -43,7 +43,7 @@ rootPb := rootNode.Builder()

lrl := sitebuilder.NewLocalResourceLocator("some/image/path")

iw := sitebuilder.NewImageWidget("image alt text 1", lrl)
iw := sitebuilder.NewImageWidget("image alt text 1", lrl, 0, 0)

err = rootPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -53,7 +53,7 @@ log.PanicIf(err)

childPb := childNode1.Builder()

iw = sitebuilder.NewImageWidget("image alt text 2", lrl)
iw = sitebuilder.NewImageWidget("image alt text 2", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -63,7 +63,7 @@ log.PanicIf(err)

childPb = childNode2.Builder()

iw = sitebuilder.NewImageWidget("image alt text 3", lrl)
iw = sitebuilder.NewImageWidget("image alt text 3", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -73,7 +73,7 @@ log.PanicIf(err)

childPb = childChildNode1.Builder()

iw = sitebuilder.NewImageWidget("image alt text 4", lrl)
iw = sitebuilder.NewImageWidget("image alt text 4", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand Down Expand Up @@ -150,7 +150,7 @@ index.html
<p><img src="file://some/image/path" alt="image alt text 1" title="image alt text 1" /></p>
<p><a href="file://example_path/child1.html">Child1</a> <a href="file://example_path/child2.html">Child2</a></p>
<p><a href="child1.html">Child1</a> <a href="child2.html">Child2</a></p>
```

(In the example above, no links were added so no links are present.)
Expand Down
11 changes: 11 additions & 0 deletions example/helloworld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Overview

This is a command-line example that'll produce a tiny, functioning static website with multiple sub-pages and embedded images.

# Running

```
$ go run main.go --output-path output
```

Then, open `output/index.html` from your browser.
Binary file added example/helloworld/asset/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/helloworld/asset/image2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/helloworld/asset/image3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions example/helloworld/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"os"
"path"

"github.com/dsoprea/go-logging"
"github.com/jessevdk/go-flags"

"github.com/dsoprea/go-static-site-builder"
"github.com/dsoprea/go-static-site-builder/markdown"
)

type rootParameters struct {
OutputPath string `long:"output-path" description:"Path to write to" required:"true"`
}

var (
rootArguments = new(rootParameters)
)

func main() {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintError(err)
os.Exit(-1)
}
}()

p := flags.NewParser(rootArguments, flags.Default)

_, err := p.Parse()
if err != nil {
os.Exit(1)
}

sc := sitebuilder.NewSiteContext(rootArguments.OutputPath)
md := markdowndialect.NewMarkdownDialect()

sb := sitebuilder.NewSiteBuilder("Site Title", md, sc)

// Create content on root page.

rootNode := sb.Root()
rootPb := rootNode.Builder()

erl1, err := sitebuilder.NewEmbeddedResourceLocator(path.Join("asset", "image1.jpg"), "", false)
log.PanicIf(err)

iw := sitebuilder.NewImageWidget("image alt text 1", erl1, 100, 100)

err = rootPb.AddContentImage(iw)
log.PanicIf(err)

// Add a new page.

childNode1, err := rootNode.AddChildNode("child1", "Child Page 1")
log.PanicIf(err)

childPb := childNode1.Builder()

erl2, err := sitebuilder.NewEmbeddedResourceLocator(path.Join("asset", "image2.jpg"), "", false)
log.PanicIf(err)

iw = sitebuilder.NewImageWidget("image alt text 2", erl2, 100, 100)

err = childPb.AddContentImage(iw)
log.PanicIf(err)

// Add a new page.

childNode2, err := rootNode.AddChildNode("child2", "Child Page 2")
log.PanicIf(err)

childPb = childNode2.Builder()

erl3, err := sitebuilder.NewEmbeddedResourceLocator(path.Join("asset", "image3.jpg"), "", false)
log.PanicIf(err)

iw = sitebuilder.NewImageWidget("image alt text 3", erl3, 100, 100)

err = childPb.AddContentImage(iw)
log.PanicIf(err)

items := []sitebuilder.LinkWidget{
sitebuilder.NewLinkWidget("Child1", sitebuilder.NewSitePageLocalResourceLocator(sb, "child1")),
sitebuilder.NewLinkWidget("Child2", sitebuilder.NewSitePageLocalResourceLocator(sb, "child2")),
}

nw := sitebuilder.NewNavbarWidget(items)

err = rootPb.AddNavbar(nw)
log.PanicIf(err)

// Render and write.

err = sb.WriteToPath()
log.PanicIf(err)

// // Print.

// files, err := ioutil.ReadDir(tempPath)
// log.PanicIf(err)

// for _, fi := range files {
// filename := fi.Name()

// fmt.Printf("%s\n", filename)
// fmt.Printf("====================\n")
// fmt.Printf("\n")

// filepath := path.Join(tempPath, filename)
// content, err := ioutil.ReadFile(filepath)
// log.PanicIf(err)

// // For the [testable] example.
// fixedContent := strings.Replace(string(content), tempPath, "example_path", -1)

// _, err = os.Stdout.Write([]byte(fixedContent))
// log.PanicIf(err)

// fmt.Printf("\n")
// }
}
19 changes: 19 additions & 0 deletions example/helloworld/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"io/ioutil"
"os/exec"
"testing"

"github.com/dsoprea/go-logging"
)

func TestMain(t *testing.T) {
tempPath, err := ioutil.TempDir("", "")
log.PanicIf(err)

cmd := exec.Command("go", "run", "main.go", "--output-path", tempPath)

err = cmd.Run()
log.PanicIf(err)
}
3 changes: 3 additions & 0 deletions example/helloworld/output/child1.html

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions example/helloworld/output/child2.html

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions example/helloworld/output/index.html

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions markdown/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMarkdownDialect_RenderIntermediate_Image(t *testing.T) {

lrl := sitebuilder.NewLocalResourceLocator("some/image/path")

iw := sitebuilder.NewImageWidget("image alt text", lrl)
iw := sitebuilder.NewImageWidget("image alt text", lrl, 0, 0)

err := pb.AddContentImage(iw)
log.PanicIf(err)
Expand Down Expand Up @@ -68,7 +68,7 @@ func ExampleMarkdownDialect_RenderHtml() {

lrl := sitebuilder.NewLocalResourceLocator("some/image/path")

iw := sitebuilder.NewImageWidget("image alt text 1", lrl)
iw := sitebuilder.NewImageWidget("image alt text 1", lrl, 0, 0)

err = rootPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -78,7 +78,7 @@ func ExampleMarkdownDialect_RenderHtml() {

childPb := childNode1.Builder()

iw = sitebuilder.NewImageWidget("image alt text 2", lrl)
iw = sitebuilder.NewImageWidget("image alt text 2", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -88,7 +88,7 @@ func ExampleMarkdownDialect_RenderHtml() {

childPb = childNode2.Builder()

iw = sitebuilder.NewImageWidget("image alt text 3", lrl)
iw = sitebuilder.NewImageWidget("image alt text 3", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand All @@ -98,7 +98,7 @@ func ExampleMarkdownDialect_RenderHtml() {

childPb = childChildNode1.Builder()

iw = sitebuilder.NewImageWidget("image alt text 4", lrl)
iw = sitebuilder.NewImageWidget("image alt text 4", lrl, 0, 0)

err = childPb.AddContentImage(iw)
log.PanicIf(err)
Expand Down Expand Up @@ -172,7 +172,7 @@ func ExampleMarkdownDialect_RenderHtml() {
//
// <p><img src="file://some/image/path" alt="image alt text 1" title="image alt text 1" /></p>
//
// <p><a href="file://example_path/child1.html">Child1</a> <a href="file://example_path/child2.html">Child2</a></p>
// <p><a href="child1.html">Child1</a> <a href="child2.html">Child2</a></p>
}

func TestMarkdownDialect_RenderIntermediate_Navbar(t *testing.T) {
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestMarkdownDialect_RenderIntermediate_Navbar(t *testing.T) {

actual := rootNode.IntermediateOutput()

expected := "# site title\n\n[Child1](file://child1.html) [Child2](file://child2.html) \n\n"
expected := "# site title\n\n[Child1](child1.html) [Child2](child2.html) \n\n"

if string(actual) != expected {
fmt.Printf("ACTUAL:\n=====\n%s=====\n", actual)
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestMarkdownDialect_RenderIntermediate_Link(t *testing.T) {

actual := rootNode.IntermediateOutput()

expected := "# site title\n\n[Child1](file://child1.html)\n\n"
expected := "# site title\n\n[Child1](child1.html)\n\n"

if string(actual) != expected {
fmt.Printf("ACTUAL:\n=====\n%s=====\n", actual)
Expand Down
20 changes: 18 additions & 2 deletions markdown/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,24 @@ import (
func ImageWidgetToMarkdown(iw sitebuilder.ImageWidget, w io.Writer) (err error) {
uri := iw.Locator.Uri()

_, err = fmt.Fprintf(w, "![%s](%s \"%s\")", iw.AltText, uri, iw.AltText)
log.PanicIf(err)
if iw.Width != 0 || iw.Height != 0 {
if iw.Width != 0 && iw.Height == 0 {
_, err = fmt.Fprintf(w, `<img src="%s" width="%d" alt="%s" />`, uri, iw.Width, iw.AltText)
log.PanicIf(err)
} else if iw.Width == 0 && iw.Height != 0 {
_, err = fmt.Fprintf(w, `<img src="%s" height="%d" alt="%s" />`, uri, iw.Height, iw.AltText)
log.PanicIf(err)
} else if iw.Width != 0 && iw.Height != 0 {
_, err = fmt.Fprintf(w, `<img src="%s" width="%d" height="%d" alt="%s" />`, uri, iw.Width, iw.Height, iw.AltText)
log.PanicIf(err)
}

_, err = fmt.Fprintf(w, "<br /><br />\n\n")
log.PanicIf(err)
} else {
_, err = fmt.Fprintf(w, "![%s](%s \"%s\")", iw.AltText, uri, iw.AltText)
log.PanicIf(err)
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion markdown/widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestImageWidgetToMarkdown(t *testing.T) {
altText := "alt text"
lrl := sitebuilder.NewLocalResourceLocator("some/image/path")

iw := sitebuilder.NewImageWidget(altText, lrl)
iw := sitebuilder.NewImageWidget(altText, lrl, 0, 0)

b := new(bytes.Buffer)

Expand Down
12 changes: 4 additions & 8 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/ioutil"
"mime"
"os"
"path"
"path/filepath"

"github.com/dsoprea/go-logging"
Expand Down Expand Up @@ -66,11 +65,8 @@ func (splrl *SitePageLocalResourceLocator) Uri() string {
log.Panicf("resource refers to invalid page-ID [%s]", splrl.PageId)
}

outputPath := splrl.sb.Context().HtmlOutputPath()
filename := splrl.sb.Context().GetFinalPageFilename(splrl.PageId)
filepath := path.Join(outputPath, filename)

return fmt.Sprintf("file://%s", filepath)
return filename
}

// Embedded data (rather than any local or remote references).
Expand All @@ -81,7 +77,7 @@ type EmbeddedResourceLocator struct {
Filepath string
}

func NewEmbeddedResourceLocator(mimeType string, raw []byte) (erl *EmbeddedResourceLocator, err error) {
func NewEmbeddedResourceLocatorWithBytes(mimeType string, raw []byte) (erl *EmbeddedResourceLocator, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
Expand Down Expand Up @@ -132,11 +128,11 @@ func NewEmbeddedResourceLocatorWithReader(mimeType string, r io.Reader) (erl *Em
return erl, nil
}

// NewEmbeddedResourceLocatorWithFilepath will read the given file and then
// NewEmbeddedResourceLocator will read the given file and then
// embed it. If `mimeType` is an empty-string, we will detect it based on the
// extension. If `readImmediately` is `false`, we'll read and embed it
// immediately rather than defer until the URI is actually requested.
func NewEmbeddedResourceLocatorWithFilepath(localFilepath, mimeType string, readImmediately bool) (erl *EmbeddedResourceLocator, err error) {
func NewEmbeddedResourceLocator(localFilepath, mimeType string, readImmediately bool) (erl *EmbeddedResourceLocator, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
Expand Down
Loading

0 comments on commit 7c26ba5

Please sign in to comment.