Skip to content

Commit

Permalink
Publish Markdown API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Mar 29, 2016
1 parent e94a3cb commit bd7d5b9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 58 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ docts
[![build status](https://travis-ci.org/charto/docts.svg?branch=master)](http://travis-ci.org/charto/docts)
[![npm version](https://img.shields.io/npm/v/docts.svg)](https://www.npmjs.com/package/docts)

API
===

>
> <a name="api-Section"></a>
> ### Class [`Section`](#api-Section)
>
> Methods:
> > **new( )** <sup>&rArr; <code>[Section](#api-Section)</code></sup>
>
> Properties:
> > **.header** <sup><code>string[]</code></sup>
> > **.content** <sup><code>string[]</code></sup>
> > **.name** <sup><code>string</code></sup>
>
> <a name="api-Markdown"></a>
> ### Class [`Markdown`](#api-Markdown)
>
> Methods:
> > **new( )** <sup>&rArr; <code>[Markdown](#api-Markdown)</code></sup>
> > &emsp;&#x25aa; markdownPath <sup><code>string</code></sup>
> > **.readSections( )** <sup>&rArr; <code>[Section](#api-Section)[]</code></sup>
> > **.writeSections( )** <sup>&rArr; <code>void</code></sup>
> > &emsp;&#x25aa; sectionList <sup><code>[Section](#api-Section)[]</code></sup>
>
> Properties:
> > **.path** <sup><code>string</code></sup>
License
=======

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"scripts": {
"tsc": "tsc",
"docts": "node docts-cli.js",
"typings": "typings",
"prepublish": "typings install && tsc",
"test": "npm link docts && docts --help"
Expand Down
69 changes: 69 additions & 0 deletions src/Markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of docts, copyright (c) 2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

import * as fs from 'fs';
import * as path from 'path';
import * as readts from 'readts';

export class Section {
header: string[] = [];
content: string[] = [];
name: string;
}

export class Markdown {
constructor(markdownPath: string) {
this.path = markdownPath;
}

readSections() {
var lineList = fs.readFileSync(this.path, { encoding: 'utf8' }).split(/\r?\n/);
var sectionList: Section[] = [];
var section = new Section();
var prev: string = null;

for(var line of lineList) {
if(line.match(/^ *[-=]{2,} *$/)) {
sectionList.push(section);
section = new Section();

if(prev) {
section.header.push(prev);
section.header.push(line);
section.name = prev.trim().toLowerCase();
}
line = null;
} else {
if(prev || prev === '') section.content.push(prev);

var match = line.match(/^ *#{1,6} *([^ #]+)/);

if(match) {
sectionList.push(section);
section = new Section();

section.header.push(line);
section.name = match[1].trim().toLowerCase();
line = null;
}
}

prev = line;
}

if(prev || prev === '') section.content.push(prev);
sectionList.push(section);

return(sectionList);
}

writeSections(sectionList: Section[]) {
var output = Array.prototype.concat.apply([], sectionList.map(
(section: Section) => section.header.concat(section.content)
)).join('\n');

fs.writeFileSync(this.path, output, { encoding: 'utf8' });
}

path: string;
}
61 changes: 4 additions & 57 deletions src/Patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as readts from 'readts';

export class Section {
header: string[] = [];
content: string[] = [];
name: string;
}
import {Section, Markdown} from './Markdown';

var hooks: readts.FormatHooks = {
ref(spec: readts.TypeSpec, hooks: readts.FormatHooks) {
Expand All @@ -20,55 +16,6 @@ var hooks: readts.FormatHooks = {
}
};

export function readSections(markdownPath: string) {
var lineList = fs.readFileSync(markdownPath, { encoding: 'utf8' }).split(/\r?\n/);
var sectionList: Section[] = [];
var section = new Section();
var prev: string = null;

for(var line of lineList) {
if(line.match(/^ *[-=]{2,} *$/)) {
sectionList.push(section);
section = new Section();

if(prev) {
section.header.push(prev);
section.header.push(line);
section.name = prev.trim().toLowerCase();
}
line = null;
} else {
if(prev || prev === '') section.content.push(prev);

var match = line.match(/^ *#{1,6} *([^ #]+)/);

if(match) {
sectionList.push(section);
section = new Section();

section.header.push(line);
section.name = match[1].trim().toLowerCase();
line = null;
}
}

prev = line;
}

if(prev || prev === '') section.content.push(prev);
sectionList.push(section);

return(sectionList);
}

function writeSections(sectionList: Section[], markdownPath: string) {
var output = Array.prototype.concat.apply([], sectionList.map(
(section: Section) => section.header.concat(section.content)
)).join('\n');

fs.writeFileSync(markdownPath, output, { encoding: 'utf8' });
}

function isIgnored(spec: readts.ClassSpec | readts.SignatureSpec | readts.IdentifierSpec) {
return(spec.doc && spec.doc.match(/@ignore/));
}
Expand Down Expand Up @@ -178,9 +125,9 @@ export function generateDoc(basePath: string) {
}

export function patchReadme(basePath: string) {
var readmePath = path.resolve(basePath, 'README.md');
var markdown = new Markdown(path.resolve(basePath, 'README.md'));

var sectionList = readSections(readmePath);
var sectionList = markdown.readSections();
var sectionTbl: { [name: string]: Section } = {};

for(var section of sectionList) sectionTbl[section.name] = section;
Expand All @@ -189,6 +136,6 @@ export function patchReadme(basePath: string) {

if(apiSection) {
apiSection.content = generateDoc(basePath);
writeSections(sectionList, readmePath);
markdown.writeSections(sectionList);
}
}
2 changes: 1 addition & 1 deletion src/docts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is part of docts, copyright (c) 2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

export {patchReadme} from './Patcher';
export {Section, Markdown} from './Markdown';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"files": [
"typings/main.d.ts",

"src/docts.ts",
"src/cli.ts"
]
}

0 comments on commit bd7d5b9

Please sign in to comment.