@jaames/cod
Advanced tools
Comparing version 1.0.0 to 1.1.0
const REGEX_NEWLINE = /\r?\n/; | ||
function extractDocComments(src, beginDoc, endDoc) { | ||
function extractDocCommentsBetween(src, beginDoc, endDoc) { | ||
let blocks = []; | ||
@@ -50,2 +50,52 @@ let inside = false; | ||
} | ||
function extractDocCommentsWithPrefix(src, docLinePrefix) { | ||
let blocks = []; | ||
let inside = false; | ||
let currDepth = 0; | ||
let currSubDepth = 0; | ||
let currBlockText = []; | ||
let currStartLine = 0; | ||
const prefixRegex = new RegExp(`^(${docLinePrefix})`); | ||
const srcLines = src.split(REGEX_NEWLINE); | ||
for (let i = 0; i < srcLines.length; i++) { | ||
const lineNum = i + 1; | ||
const line = srcLines[i]; | ||
// outside a block | ||
if (!inside) { | ||
// currDepth = line.indexOf(docLinePrefix); | ||
// block start | ||
if (line.startsWith(docLinePrefix)) { | ||
currDepth = 0; | ||
inside = true; | ||
currBlockText = []; | ||
currStartLine = lineNum; | ||
} | ||
} | ||
// inside block | ||
if (inside) { | ||
const idx = line.indexOf(docLinePrefix); | ||
// block end | ||
if (idx == -1) { | ||
inside = false; | ||
blocks.push({ | ||
body: currBlockText, | ||
lineBefore: currStartLine - 1, | ||
lineAfter: lineNum | ||
}); | ||
currBlockText = []; | ||
currStartLine = 0; | ||
} | ||
else { | ||
let substr = line.replace(prefixRegex, '').substring(currDepth); | ||
const search = substr.search(/[^ ]/); | ||
if (search >= 0) | ||
currSubDepth = search; | ||
else if (substr.length < currSubDepth) | ||
substr = ' '.repeat(currSubDepth + 1); | ||
currBlockText.push(substr); | ||
} | ||
} | ||
} | ||
return blocks; | ||
} | ||
@@ -183,8 +233,15 @@ var ParsedLineType; | ||
docBegin: '/**', | ||
docEnd: 's*/' | ||
docEnd: 's*/', | ||
docLinePrefix: '' | ||
}, config); | ||
const blocks = extractDocComments(text, opts.docBegin, opts.docEnd); | ||
return blocks.map(parseCommentBlock); | ||
if (opts.docLinePrefix) { | ||
const blocks = extractDocCommentsWithPrefix(text, opts.docLinePrefix); | ||
return blocks.map(parseCommentBlock); | ||
} | ||
else { | ||
const blocks = extractDocCommentsBetween(text, opts.docBegin, opts.docEnd); | ||
return blocks.map(parseCommentBlock); | ||
} | ||
} | ||
export { ParsedLineType, cod, extractDocComments, parseCommentBlock }; | ||
export { ParsedLineType, cod, extractDocCommentsBetween, extractDocCommentsWithPrefix, parseCommentBlock }; |
{ | ||
"name": "@jaames/cod", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "An unopinionated documentation generator.", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"main": "dist/cod.js", | ||
"module": "dist/cod.js", | ||
"bin": { | ||
@@ -12,3 +12,3 @@ "cod": "dist/cli.js" | ||
"build": "rollup -c --environment BUILD:production", | ||
"build+run": "rollup -c --environment BUILD:production && node ./dist/cli.js ./test.js" | ||
"buildrun": "rollup -c --environment BUILD:production && node ./dist/cli.js ./test.js" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": { |
@@ -14,2 +14,3 @@ <p align="center"> | ||
- Add line number output | ||
- Add support for languages where doc comment lines instead begin with a given prefix, like Emmylua annotations. | ||
@@ -44,3 +45,3 @@ A couple of things I didn't bother porting as they weren't relevant to me, feel free to make a PR though: | ||
{ | ||
"Example": { | ||
"@Example": { | ||
"lineBefore": 0, | ||
@@ -183,2 +184,3 @@ "lineAfter": 15, | ||
cod -b '--[[*' -e ']]' *.lua | ||
cod -p '---' *.lua | ||
``` | ||
@@ -205,2 +207,3 @@ | ||
-e <doc-end> String that marks the end of a doc-block (default: "*/") | ||
-p <doc-line-prefix> String that marks the line prefix of a doc-block (default: "", doc-begin and doc-end are ignored if used) | ||
-o <output-file> Output file (default: "STDOUT") | ||
@@ -232,2 +235,6 @@ -u --ugly Output non-pretty JSON. | ||
> > > String that marks the end of a doc-block | ||
> | ||
> > <a name='api_cod_options_ docLinePrefix'></a> | ||
> > [`docLinePrefix`](#api_cod_options_ docLinePrefix) (String) default: `""` | ||
> > > String that marks the line prefix of a doc-block - [`docBegin`](#api_cod_options_docBegin) and [`docEnd`](#api_cod_options_docEnd) are ignored if used. | ||
@@ -234,0 +241,0 @@ ```js |
@@ -20,6 +20,7 @@ import fs from 'fs'; | ||
.showHelpAfterError('(add --help for additional information)') | ||
.option('-b <doc-begin>', 'String that marks the start of a doc-block', '/**') | ||
.option('-e <doc-end>', 'String that marks the end of a doc-block', '*/') | ||
.option('-o <output-file>', 'Output file', 'STDOUT') | ||
.option('-u --ugly', 'Output non-pretty JSON.', false) | ||
.option('-b <doc-begin>', 'String that marks the start of a doc-block', '/**') | ||
.option('-e <doc-end>', 'String that marks the end of a doc-block', '*/') | ||
.option('-p <doc-line-prefix>', 'String that marks the line prefix of a doc-block', '') | ||
.option('-o <output-file>', 'Output file', 'STDOUT') | ||
.option('-u --ugly', 'Output non-pretty JSON.', false) | ||
.version(pkg.version, '-v --version', 'output the current version'); | ||
@@ -40,2 +41,3 @@ | ||
docEnd: options.e, | ||
docLinePrefix: options.p, | ||
}); | ||
@@ -42,0 +44,0 @@ const formatted = options.u ? JSON.stringify(transformed) : JSON.stringify(transformed, null, 2); |
@@ -9,3 +9,3 @@ const REGEX_NEWLINE = /\r?\n/; | ||
export function extractDocComments(src: string, beginDoc: string, endDoc: string) { | ||
export function extractDocCommentsBetween(src: string, beginDoc: string, endDoc: string) { | ||
let blocks: CommentBlock[] = []; | ||
@@ -57,2 +57,53 @@ let inside = false; | ||
return blocks; | ||
} | ||
export function extractDocCommentsWithPrefix(src: string, docLinePrefix: string) { | ||
let blocks: CommentBlock[] = []; | ||
let inside = false; | ||
let currDepth = 0; | ||
let currSubDepth = 0; | ||
let currBlockText: string[] = []; | ||
let currStartLine = 0; | ||
const prefixRegex = new RegExp(`^(${docLinePrefix})`); | ||
const srcLines = src.split(REGEX_NEWLINE); | ||
for (let i = 0; i < srcLines.length; i++) { | ||
const lineNum = i + 1; | ||
const line = srcLines[i]; | ||
// outside a block | ||
if (!inside) { | ||
// currDepth = line.indexOf(docLinePrefix); | ||
// block start | ||
if (line.startsWith(docLinePrefix)) { | ||
currDepth = 0 | ||
inside = true; | ||
currBlockText = []; | ||
currStartLine = lineNum; | ||
} | ||
} | ||
// inside block | ||
if (inside) { | ||
const idx = line.indexOf(docLinePrefix); | ||
// block end | ||
if (idx == -1) { | ||
inside = false; | ||
blocks.push({ | ||
body: currBlockText, | ||
lineBefore: currStartLine - 1, | ||
lineAfter: lineNum | ||
}); | ||
currBlockText = []; | ||
currStartLine = 0; | ||
} | ||
else { | ||
let substr = line.replace(prefixRegex, '').substring(currDepth); | ||
const search = substr.search(/[^ ]/); | ||
if (search >= 0) | ||
currSubDepth = search; | ||
else if (substr.length < currSubDepth) | ||
substr = ' '.repeat(currSubDepth + 1); | ||
currBlockText.push(substr); | ||
} | ||
} | ||
} | ||
return blocks; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { extractDocComments } from './extract'; | ||
import { extractDocCommentsBetween, extractDocCommentsWithPrefix } from './extract'; | ||
import { parseCommentBlock } from './parser'; | ||
@@ -9,3 +9,4 @@ | ||
docBegin: string, | ||
docEnd: string | ||
docEnd: string, | ||
docLinePrefix: string, | ||
}; | ||
@@ -16,6 +17,13 @@ | ||
docBegin: '/**', | ||
docEnd: 's*/' | ||
docEnd: 's*/', | ||
docLinePrefix: '' | ||
}, config); | ||
const blocks = extractDocComments(text, opts.docBegin, opts.docEnd); | ||
return blocks.map(parseCommentBlock); | ||
if (opts.docLinePrefix) { | ||
const blocks = extractDocCommentsWithPrefix(text, opts.docLinePrefix) | ||
return blocks.map(parseCommentBlock); | ||
} | ||
else { | ||
const blocks = extractDocCommentsBetween(text, opts.docBegin, opts.docEnd); | ||
return blocks.map(parseCommentBlock); | ||
} | ||
} |
@@ -19,8 +19,4 @@ { | ||
"*" : ["src/types/*"] | ||
}, | ||
"typeRoots": [ | ||
"./src/custom.d.ts", | ||
"node_modules/@types", | ||
] | ||
} | ||
} | ||
} |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
133731
3658
259
11