Giter VIP home page Giter VIP logo

jroff's Introduction

jroff

travis Code Climate Test Coverage

About

For a real-life example of Jroff in action, check out grapse.

Jroff is a man page parser written in JavaScript, featuring:

  • Partial support for groff-native commands
  • Full support for an macros
  • Full support for doc macros

The main functionality of the library is to produce an AST ready to be consumed by a generator.

At the moment, only a single HTML generator has been implemented, but the plan is to implement several more in the near future.

Usage

An annotated version of the code can be found here

HTML Generator

Using the generator is fairly simple. You only have to create a new instance and call the generate method every time you want to parse a string containing groff text.

The generate function takes two arguments: the source string and an optional name from the macro library that will be used to parse the result:

// Instantiate a new generator
var generator = new Jroff.HTMLGenerator();
// Generate the HTML output
var result = generator.generate('.Op Fl 0 Ns Op Ar octal', 'doc');

This is what we see when we print the result variable:

<span>[<strong>-0 <span>[<i>octal</i>]</span></strong>]</span>

Parser

The parser takes a string as a source, being capable of building an AST from it. Using the parser is very straightforward, but it is nearly useless without a generator:

// Instantiate a new parser class
var parser = new Jroff.Parser('.Op Fl lorem ipsum');
// Build the AST
var ast = parser.buildAST();

This is what we see when we inspect the ast variable:

[{
  "value": "Op",
  "kind": 2,
  "nodes": [{
    "value": " ",
    "kind": 5,
    "nodes": []
  }, {
    "value": "Fl",
    "kind": 3,
    "nodes": [{
      "value": " lorem ipsum",
      "kind": 5,
      "nodes": []
    }]
  }]
}]

Defining New Macros

You can define a specific macro for your project by adding a new item in the Jroff.macros.defaults object, using the macro name as a key and the function with a specific macro functionality as the value:

Jroff.macros.defaults.sp = function (spacing) {
    spacing = spacing || '2';
    return '<hr style="margin-top:' + spacing + 'em;visibility:hidden;">';
};

Internally, all macros are defined like this. You can check out src/macros/defaults.js for more examples.

Contributing

For regular development, you need to have Node.js >=0.10 installed in your system. Then,

Clone the repository.

git clone [email protected]:roperzh/jroff_final.git
cd jroff_final

Install the required dependencies.

npm install

Finally, build your local copy and run the tests.

make all

Brief Description of the Organization of the Code

dist: This folder stores the bundled versions of the code.

src/core: As the name of the folder suggests, this is the core of the library. There is no HTML-related code here, only code to parse and build the AST.

src/generators: Generators are entities that can consume the AST generated by the parser and produce an output. At the moment, only one HTML generator has been implemented.

src/macros: Stores macro commands and macro packages. At the moment, it only supports an and doc packages. Macros supported by groff are stored in default.js

src/utils: Utility functions and miscellany.

Brief Description of Make Commands

make dist: beautifies and builds minified and concatenated versions of the code.

make hint: runs eslint over the test files and the concatenated, non-minified version of the code.

make beautify: runs js-beautify over all JavaScript files.

make doc: generates local documentation based on the current version of the code. This is useful for previewing documentation before publishing it. See the next section for more details.

Documenting New Code

This library uses jsdoc3 to generate documentation, so all new code must be documented using jsdoc tags. A useful reference can be found here.

Also, due to some limitations with jsdoc and UMD, the @alias tag must be added to all functions, constructors, and namespaces manually.

Generating Documentation

The Makefile includes a useful command that generates and pushes the documentation onto GitHub and GitHub pages. You can simply run:

make doc-build

Note: Please make sure to stash or commit all your changes before generating the documentation.

If you want to preview the changes before pushing the documentation, you can generate it with the doc task and then open docs/index.html on your browser.

An example of this using OS X:

make doc
open docs/index.html

Benchmarks

Running benchmarks is a complicated task, even more so in this case since there aren't other libraries to use as a reference and compare the results with.

Therefore, what these benchmarks are for is to compare different versions of the library with three arbitrary man pages: Git, Node.js, and Ruby

This benchmark should be used only for drawing estimative comparisons between versions or complex features.

If you want to run the benchmarks and compare the new data with the latest stored results:

make bench

Alternatively, you can store the results of the benchmark in the cache file (benchmarks/benchmarks.json) with an -s flag:

make bench flags='-s'

To-Dos

  • Add support for nested numeric lists
  • Add support for -width arguments in lists
  • Add missing macros

Links

License

All the code contained in this repository, unless explicitly stated, is licensed under an MIT license.

A copy of the license can be found in the LICENSE file.

jroff's People

Contributors

chrismwendt avatar roperzh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jroff's Issues

Is '&minus;' the correct symbol for options?

I expected to find '-' for options instead of '−'. For instance,

.TP
\fB\  \fR\-\-cache\-strategy strategy

is rendered

−−cache −strategy strategy

while online manuals report --cache-strategy strategy

Wrongly added space before symbol

Test example (wrong rendering):
https://ircama.github.io/osm-carto-tutorials/manpage.html?url=https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1

Correctly rendered page:
https://manned.org/osm2pgsql

Issues:

Correct: -O|--output
Wrong: −O |−−output

Error description: no space should be added within O |

Correct: back-end
Wrong: back −end

Error description: no space should be added within k −

Correct: osm2pgsql supports pgsql, gazetteer and null.
Wrong: osm2pgsql supports pgsql , gazetteer and null .

Error description: no space should be added within l , or within l .

Other example:

.TP
\fB\  \fR\-\-cache\-strategy strategy

is rendered

−−cache −strategy strategy

instead of

 --cache-strategy strategy

Standalone .Fa causes the parser to crash

A standalone .Fa macro, which is not embedded in .Fo / .Fc, causes the parser to crash with TypeError: Cannot read property 'push' of undefined.

As an example consider the following excerpt from the unlink(2) system call.

The
.Fn unlink
function
removes the link named by
.Fa path
from its directory and decrements the link count of the
file which was referenced by the link.

Missing .TP macro

I suggest to add the styling of the .TP macro, which is for instance used when reporting command line options.

Example:

.TP
\fB\-a\fR|\-\-append
Add the OSM file into the database without removing
existing data.

Notice that the newline between the option and its description might be kept. The following trivial example includes a very basic (and incorrect) processing of the .TP macro, without breaking the lines.

https://ircama.github.io/osm-carto-tutorials/manpage.html?url=https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1

Correctly rendered page:
https://manned.org/osm2pgsql

Spaces should not be removed when more than one is used

Test example (wrong rendering):
https://ircama.github.io/osm-carto-tutorials/manpage.html?url=https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1

Correctly rendered page:
https://manned.org/osm2pgsql

Issue:

Correct:       Latlong             (-l) SRS:  4326 (none)
Wrong: Latlong ( −l) SRS: 4326 (none)

Error description: when more than one space is used, spaces might not be removed (and possibly the font might be switched to monospaced; e.g., <pre> tag).

Wrongly removed space after symbols

Test example (wrong rendering):
https://ircama.github.io/osm-carto-tutorials/manpage.html?url=https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1

Correctly rendered page:
https://manned.org/osm2pgsql

Issue:

.SH NAME
osm2pgsql \- Openstreetmap data to PostgreSQL converter.

Converted form: osm2pgsql −Openstreetmap data to PostgreSQL converter.
Space is wrongly removed within −O.

Other example of removed space after ]:

.RI [ options ] " planet.osm"

Converted form: osm2pgsql [options]planet.osm
Space is wrongly removed within ]p.

Online man page styler from a URL of your choice

Congratulations for your work.

I would suggest to add a github page to your repository, which can be used to allow rendering the man page of any troff document available on the web by appending the related URL as parameter to that page, like this:

https://ircama.github.io/osm-carto-tutorials/manpage.html?url=https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1

In this very rough example (to be removed as soon as a better solution will be available), the rendered document is for instance https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/docs/osm2pgsql.1
The github page is https://github.com/Ircama/osm-carto-tutorials/blob/gh-pages/manpage.html

The inspiration for this idea is from https://matteobrusa.github.io/md-styler/

BTW, while testing some man pages with this example, I found some minor errors which I will report to you through specific github issues.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.