Skip to content

Commit

Permalink
refactor command and update doc
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Apr 18, 2018
1 parent 8784ace commit 99e59ec
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ tmp
# exclude code coverage merged results
coverage.txt
*.html
.DS_Store
21 changes: 17 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ It is is available under the terms of the https://raw.githubusercontent.com/byte
* Inline images in paragraphs (`image://`)
* Block images (`image:://`)
* Element attributes (`ID`, `link` and `title`, where applicable) on block images, paragraphs, lists and sections
* Labeled, ordered and unordered lists (with nesting and attributes)


See the http://LIMITATIONS.adoc[known limitations] page for differences between Asciidoc/Asciidoctor and Libasciidoc.
Expand All @@ -33,18 +34,30 @@ Further elements will be supported in the future. Feel free to open issues https

== Usage

=== Command Line

The libasciidoc library includes a minimalist command line interface to generate the HTML content from a given file:

```
$ libasciidoc -s content.adoc
```

=== Code integration

Libasciidoc provides 2 functions to convert an Asciidoc content into HTML:

1. Converting to a complete HTML document:
1. Converting an `io.Reader` into an HTML document:

func ConvertToHTML(context.Context, io.Reader, io.Writer, renderer.Option...) (map[string]interface{}, error)
func ConvertToHTML(ctx context.Context, source io.Reader, output io.Writer, options renderer.Option...) (map[string]interface{}, error)

2. Converting to a `body` element only:
2. Converting a file (giving its name) into an HTML document:

func ConvertToHTMLBody(context.Context, io.Reader, io.Writer) (map[string]interface{}, error)
func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, options renderer.Option...) (map[string]interface{}, error)

where the returned `map[string]interface{}` object contains the document's title (which is not rendered in the HTML's body) and its other attributes.

The currently available option to pass as a last argument is `renderer.IncludeHeaderFooter(false)` to limit the generation to the body of the HTML document.

== How to contribute

Please refer to the http://CONTRIBUTE.adoc[Contribute] page.
22 changes: 3 additions & 19 deletions cmd/libasciidoc/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package main

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/bytesparadise/libasciidoc"
"github.com/bytesparadise/libasciidoc/renderer"
"github.com/bytesparadise/libasciidoc/renderer/html5"
"github.com/bytesparadise/libasciidoc/types"

"github.com/bytesparadise/libasciidoc/parser"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,21 +43,10 @@ func newRootCmd() *cobra.Command {
os.Exit(1)
}
source := cmd.Flag("source").Value.String()
b, err := ioutil.ReadFile(source)
_, err := libasciidoc.ConvertFileToHTML(context.Background(), source, os.Stdout, renderer.IncludeHeaderFooter(true)) //renderer.IncludeHeaderFooter(true)
if err != nil {
fmt.Printf("failed to read the source file: %v\n", err)
os.Exit(1)
}
doc, err := parser.Parse(source, b)
if err != nil {
fmt.Printf("failed to parse the source file: %v\n", err)
os.Exit(1)
return err
}
buff := bytes.NewBuffer(nil)
actualDocument := doc.(types.Document)
rendererCtx := renderer.Wrap(context.Background(), actualDocument)
_, err = html5.Render(rendererCtx, buff)
fmt.Printf("%s\n", buff.String())
return nil
},
}
Expand Down
28 changes: 12 additions & 16 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,29 @@ import (
log "github.com/sirupsen/logrus"
)

// ConvertToHTMLBody converts the content of the given reader `r` into an set of <DIV> elements for an HTML/BODY document.
// The conversion result is written in the given writer `w`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned
// ConvertFileToHTML converts the content of the given filename into an HTML document.
// The conversion result is written in the given writer `output`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned
// as the result of the function call.
func ConvertToHTMLBody(ctx context.Context, r io.Reader, w io.Writer) (map[string]interface{}, error) {
doc, err := parser.ParseReader("", r)
func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
doc, err := parser.ParseFile(filename)
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
options := []renderer.Option{renderer.IncludeHeaderFooter(false)}
metadata, err := htmlrenderer.Render(renderer.Wrap(ctx, doc.(types.Document), options...), w)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering the document")
}
log.Debugf("Done processing document")
return metadata, nil
return convertToHTML(ctx, doc, output, options...)
}

// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `w`.
// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `output`.
// Returns an error if a problem occurred
func ConvertToHTML(ctx context.Context, r io.Reader, w io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
func ConvertToHTML(ctx context.Context, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
doc, err := parser.ParseReader("", r)
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
// force/override value
options = append(options, renderer.IncludeHeaderFooter(true))
metadata, err := htmlrenderer.Render(renderer.Wrap(ctx, doc.(types.Document), options...), w)
return convertToHTML(ctx, doc, output, options...)
}

func convertToHTML(ctx context.Context, doc interface{}, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
metadata, err := htmlrenderer.Render(renderer.Wrap(ctx, doc.(types.Document), options...), output)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering the document")
}
Expand Down
2 changes: 1 addition & 1 deletion libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func verifyDocumentBody(t GinkgoTInterface, expectedRenderedTitle *string, expec
t.Logf("processing '%s'", source)
sourceReader := strings.NewReader(source)
resultWriter := bytes.NewBuffer(nil)
metadata, err := ConvertToHTMLBody(context.Background(), sourceReader, resultWriter)
metadata, err := ConvertToHTML(context.Background(), sourceReader, resultWriter, renderer.IncludeHeaderFooter(false))
require.Nil(t, err, "Error found while parsing the document")
require.NotNil(t, metadata)
t.Log("Done processing document")
Expand Down

0 comments on commit 99e59ec

Please sign in to comment.