Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attributes argument of render/load APIs to be a string or array #289

Closed
mojavelinux opened this issue Apr 27, 2013 · 0 comments
Closed
Assignees
Milestone

Comments

@mojavelinux
Copy link
Member

Currently, attributes must be passed as a Hash to the render and load APIs. There are times when this makes the argument list feel too heavy. As an alternative, these APIs should accept the attributes as a string or array.

For instance, consider a user of the API wants to pass the attribute that enables the table of contents (toc) and auto-numbering (numbered).

result = Asciidoctor.render_file('master.ad',
    :attributes => {'toc' => '', 'numbered' => ''})

The toc attribute does not require a value. An empty string value is the convention in AsciiDoc/Asciidoctor to enable an attribute of this type (a flag attribute).

This invocation could be simplified if the API accepted the attribute keys as an array.

result = Asciidoctor.render_file('master.ad',
    :attributes => ['toc', 'numbered'])

It can be further simplified using the array-from-string shorthand in Ruby:

result = Asciidoctor.render_file('master.ad',
    :attributes => %w(toc numbered))

That leads us into attributes specified as a string:

result = Asciidoctor.render_file('master.ad',
    :attributes => 'toc numbered')

That's quite an improvement over the original call.

To handle attributes that accept values, we can recognize the key=value form:

result = Asciidoctor.render_file('master.ad',
    :attributes => 'toc numbered source-highlighter=coderay')

This parallels the commandline argument form, encouraging consistency:

asciidoctor -a toc -a numbered -a source-highlighter=coderay master.adoc

I think this enhancement will be particularly useful in the integrations, such as the Gradle plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant