diff --git a/.gitignore b/.gitignore index be39ad33..b73fcb21 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ tmp # exclude code coverage merged results coverage.txt *.html +.DS_Store diff --git a/README.adoc b/README.adoc index cc42a29f..7dfbcbdd 100644 --- a/README.adoc +++ b/README.adoc @@ -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. @@ -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. diff --git a/cmd/libasciidoc/main.go b/cmd/libasciidoc/main.go index 99c16ecd..f1052210 100644 --- a/cmd/libasciidoc/main.go +++ b/cmd/libasciidoc/main.go @@ -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" @@ -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 }, } diff --git a/libasciidoc.go b/libasciidoc.go index 103ee67c..c9b75ef8 100644 --- a/libasciidoc.go +++ b/libasciidoc.go @@ -12,33 +12,29 @@ import ( log "github.com/sirupsen/logrus" ) -// ConvertToHTMLBody converts the content of the given reader `r` into an set of
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") } diff --git a/libasciidoc_test.go b/libasciidoc_test.go index f73aabd3..36a7768a 100644 --- a/libasciidoc_test.go +++ b/libasciidoc_test.go @@ -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")