Giter VIP home page Giter VIP logo

code-part's Introduction

code-part

npm version Build Status Coverage Status Dependency Status devDependency Status

Parts comments and code into a data structure with htmlParser2 for html and line based comment parsing for everything else. code-part tracks starting line numbers for each code section so it's possible to add line numbers if you plan to use a syntax highlighter like google-code-prettify to display your code.

code-part's code for line based parsing was modified from docco. This code base does not include docco. The only current dependencies are htmlParser2 and lodash.

Comment support for everything except html only extracts comments at the beginning of the line and only the single line version of the languages comment. For example /* not extracted */ does not get extracted but // extracted does.

To see a list of the languages supported by the line based parsing have a look at resources/languages.json.

Usage

  var part = require('code-part');

  // Path is used to decide which parser to use
  // (html or lineBased currently) and decides comment
  // parsing in lineBased.
  var sections = part(path, code, config);

  // If code is null, the specified path is assumed
  // to be a path on the file system and is read in
  // with `readFileSync`.

Configuration

  var section = part(path, code, {
    // Options used when parsing html.
    // By default the parser will skip comments that start with
    // `<!--[`. Set to `true` to include these as comments.
    noSkipDirectives: false, // default

    // Used instead of path's extension when determining
    // the parser (html or lineBased). Also used in the
    // lineBased parser.
    // when looking up comment markers and deciding if it
    // is literate (litcoffee).
    extension: '.css'
  });

Output

  • input:
// comment 1
var code = 1;
// comment 2
if (code) code += 1
  • output:
[ { docsText: 'comment 1\n',
    docsLine: 1,
    codeText: 'var code = 1;\n',
    codeLine: 2 },
  { docsText: 'comment 2\n',
    docsLine: 3,
    codeText: 'if (code) code += 1\n',
    codeLine: 4 } ]
  • input:
<html>
  <!-- title part -->
  <head><title> title </title></head>
<body>
  <!-- main body -->
  <h1>hello world</h1>
<!-- the end -->
</body>
</html>

output:

[ { docsText: '',
    docsLine: 1,
    codeText: '<html>\n  ',
    codeLine: 1 },
  { docsText: 'title part',
    docsLine: 2,
    codeText: '\n  <head><title> title </title></head>\n<body>\n  ',
    codeLine: 3 },
  { docsText: 'main body',
    docsLine: 5,
    codeText: '\n  <h1>hello world</h1>\n',
    codeLine: 6 },
  { docsText: 'the end',
    docsLine: 7,
    codeText: '\n</body>\n</html>\n',
    codeLine: 8 } ]

BUGS

It is possible to use jade, but multi-line comments are not supported.

Supported:

html
  // comment 1
  head
    // comment 2
    title A Title

Not Supported:

html
  //
    This is a
    multi-line comment
    and is not supported
  head
    title Another Title

TODO

  • Multi-line comment parsing like jade comments.

LICENSE

Copyright (C) 2014 Scott Beck, all rights reserved

Licensed under the MIT license

code-part's People

Contributors

bline avatar

Watchers

 avatar

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.