@@ -3,7 +3,7 @@ var handlebars = require('handlebars'),
33 fs = require ( 'fs' ) ,
44 path = require ( 'path' ) ,
55 wrench = require ( 'wrench' ) ,
6- parser = require ( './parser' ) ,
6+ Parser = require ( './parser' ) ,
77 pathProcessor = require ( './pathProcessor' ) ;
88
99
@@ -14,99 +14,91 @@ var DEFAULT_INCLUDE = '*.mdown,*.md,*.markdown',
1414 DEFAULT_PAGE_TITLE = 'Documentation' ;
1515
1616
17- // ---
18-
19- var _baseTemplatePath ,
20- _docTemplate ,
21- _sidebarTemplate ,
22- _indexTemplate ;
17+ var Writter = function ( config ) {
18+ this . config = config ;
19+ this . parser = new Parser ( this . config ) ;
20+ }
2321
24- function compileTemplate ( name ) {
25- var tmp = path . normalize ( _baseTemplatePath + '/' + name + '.hbs' ) ;
22+ Writter . prototype . compileTemplate = function ( name ) {
23+ var tmp = path . normalize ( this . baseTemplatePath + '/' + name + '.hbs' ) ;
2624 return handlebars . compile ( fs . readFileSync ( tmp , 'utf-8' ) ) ;
2725}
2826
27+ Writter . prototype . compileAllTemplates = function ( ) {
28+ this . baseTemplatePath = this . config . templatePath || __dirname + '/../template' ;
2929
30- function compileAllTemplates ( config ) {
31- _baseTemplatePath = config . templatePath || __dirname + '/../template' ;
32-
33- var key , helpers = config . hbHelpers ;
30+ var key , helpers = this . config . hbHelpers ;
3431 for ( key in helpers ) {
3532 if ( helpers . hasOwnProperty ( key ) ) {
3633 handlebars . registerHelper ( key , helpers [ key ] ) ;
3734 }
3835 }
3936
40- handlebars . registerPartial ( 'header' , compileTemplate ( 'header' ) ) ;
41- handlebars . registerPartial ( 'footer' , compileTemplate ( 'footer' ) ) ;
37+ handlebars . registerPartial ( 'header' , this . compileTemplate ( 'header' ) ) ;
38+ handlebars . registerPartial ( 'footer' , this . compileTemplate ( 'footer' ) ) ;
4239
43- _docTemplate = compileTemplate ( 'doc' ) ;
44- _sidebarTemplate = compileTemplate ( 'sidebar' ) ;
45- _indexTemplate = compileTemplate ( 'index' ) ;
40+ this . docTemplate = this . compileTemplate ( 'doc' ) ;
41+ this . sidebarTemplate = this . compileTemplate ( 'sidebar' ) ;
42+ this . indexTemplate = this . compileTemplate ( 'index' ) ;
4643}
4744
48-
49- // ---
50-
51-
52- exports . processFiles = function ( config ) {
45+ Writter . prototype . processFiles = function ( ) {
5346 console . log ( ' Converting files...' ) ;
5447
55- compileAllTemplates ( config ) ;
48+ this . compileAllTemplates ( this . config ) ;
5649
57- var toc = processDoc ( config ) ,
58- outputDir = config . outputDir ;
50+ var toc = this . processDoc ( this . config ) ,
51+ outputDir = this . config . outputDir ;
5952
6053 console . log ( ' Generating Sidebar...' ) ;
61- fs . writeFileSync ( path . join ( outputDir , 'sidebar_.html' ) , _sidebarTemplate ( {
54+ fs . writeFileSync ( path . join ( outputDir , 'sidebar_.html' ) , this . sidebarTemplate ( {
6255 modules : toc ,
63- ctx : config . ctx || { }
56+ ctx : this . config . ctx || { }
6457 } ) , 'utf-8' ) ;
6558
6659 console . log ( ' Generating Index...' ) ;
67- fs . writeFileSync ( path . join ( outputDir , 'index.html' ) , _indexTemplate ( {
60+ fs . writeFileSync ( path . join ( outputDir , 'index.html' ) , this . indexTemplate ( {
6861 modules : toc ,
69- page_title : config . baseTitle || DEFAULT_PAGE_TITLE ,
70- content : getIndexContent ( config ) ,
71- ctx : config . ctx || { }
62+ page_title : this . config . baseTitle || DEFAULT_PAGE_TITLE ,
63+ content : this . getIndexContent ( this . config ) ,
64+ ctx : this . config . ctx || { }
7265 } ) , 'utf-8' ) ;
7366
7467 console . log ( ' Copying Assets...' ) ;
75- var assetsPath = config . assetsPath || path . normalize ( _baseTemplatePath + '/assets_' ) ;
68+ var assetsPath = this . config . assetsPath || path . normalize ( this . baseTemplatePath + '/assets_' ) ;
7669 wrench . copyDirSyncRecursive ( assetsPath , path . join ( outputDir , 'assets_/' ) ) ;
7770
7871 console . log ( ' Finished.' ) ;
7972} ;
8073
81-
82- function processDoc ( config ) {
83-
74+ Writter . prototype . processDoc = function ( ) {
8475 var toc = [ ] ;
76+ var self = this ;
8577
86- getFilesInfos ( config ) . forEach ( function ( fileInfo ) {
78+ this . getFilesInfos ( this . config ) . forEach ( function ( fileInfo ) {
8779
88- if ( config . mapOutName ) {
89- fileInfo . output = config . mapOutName ( fileInfo . output ) ;
80+ if ( self . config . mapOutName ) {
81+ fileInfo . output = self . config . mapOutName ( fileInfo . output ) ;
9082 }
9183
9284 pathProcessor . processFile ( fileInfo , function ( content ) {
93- var parseResult = parser . parseDoc ( content , config . headingLevel ) ,
94- fileName = fileInfo . output . replace ( config . outputDir , '' ) . replace ( / ^ [ \/ \\ ] / , '' ) ,
95- moduleName = config . mapTocName ? config . mapTocName ( fileName , parseResult . toc , parseResult . title ) : fileName . replace ( '.html' , '' ) ;
85+ var parseResult = self . parser . parseDoc ( content , self . config . headingLevel ) ,
86+ fileName = fileInfo . output . replace ( self . config . outputDir , '' ) . replace ( / ^ [ \/ \\ ] / , '' ) ,
87+ moduleName = self . config . mapTocName ? self . config . mapTocName ( fileName , parseResult . toc , parseResult . title ) : fileName . replace ( '.html' , '' ) ;
9688
9789 toc . push ( {
9890 'file' : fileName ,
9991 'module' : moduleName ,
10092 'toc' : parseResult . toc
10193 } ) ;
10294
103- var relativeRoot = path . relative ( fileInfo . output . replace ( / [ \/ \\ ] [ ^ \/ \\ ] + $ / , '/' ) , config . outputDir ) ;
95+ var relativeRoot = path . relative ( fileInfo . output . replace ( / [ \/ \\ ] [ ^ \/ \\ ] + $ / , '/' ) , self . config . outputDir ) ;
10496
105- return _docTemplate ( {
97+ return self . docTemplate ( {
10698 root_path : relativeRoot ? relativeRoot + '/' : '' ,
107- content : handlebars . compile ( parseResult . html ) ( { ctx : config . ctx } ) ,
108- page_title : parseResult . title + ' : ' + ( config . baseTitle || DEFAULT_PAGE_TITLE ) ,
109- ctx : config . ctx || { }
99+ content : handlebars . compile ( parseResult . html ) ( { ctx : self . config . ctx } ) ,
100+ page_title : parseResult . title + ' : ' + ( self . config . baseTitle || DEFAULT_PAGE_TITLE ) ,
101+ ctx : self . config . ctx || { }
110102 } ) ;
111103 } ) ;
112104 console . log ( ' processed: ' + fileInfo . input + ' > ' + fileInfo . output ) ;
@@ -115,36 +107,35 @@ function processDoc(config){
115107 return toc ;
116108}
117109
118-
119-
120- function getFilesInfos ( config ) {
110+ Writter . prototype . getFilesInfos = function ( ) {
121111 var files = pathProcessor . getFilesPaths ( {
122- inputDir : config . inputDir ,
123- outputDir : config . outputDir ,
112+ inputDir : this . config . inputDir ,
113+ outputDir : this . config . outputDir ,
124114 outputExt : '.html' ,
125- include : config . include || DEFAULT_INCLUDE ,
126- exclude : config . exclude
115+ include : this . config . include || DEFAULT_INCLUDE ,
116+ exclude : this . config . exclude
127117 } ) ;
128118
129- if ( config . filterFiles ) {
130- files = files . filter ( config . filterFiles ) ;
119+ if ( this . config . filterFiles ) {
120+ files = files . filter ( this . config . filterFiles ) ;
131121 }
132122
133123 return files ;
134124}
135125
136-
137- function generateFile ( toc , template , outputFile , title ) {
126+ Writter . prototype . generateFile = function ( toc , template , outputFile , title ) {
138127 var content = template ( {
139128 modules : toc ,
140129 page_title : title || ''
141130 } ) ;
142131 fs . writeFileSync ( outputFile , content , 'utf-8' ) ;
143132}
144133
145- function getIndexContent ( config ) {
146- if ( config . indexContentPath && ! config . indexContent ) {
147- config . indexContent = handlebars . compile ( parser . parseMdown ( fs . readFileSync ( config . indexContentPath , 'utf-8' ) ) ) ( { ctx : config . ctx } ) ;
134+ Writter . prototype . getIndexContent = function ( ) {
135+ if ( this . config . indexContentPath && ! this . config . indexContent ) {
136+ this . config . indexContent = handlebars . compile ( this . parser . parseMdown ( fs . readFileSync ( this . config . indexContentPath , 'utf-8' ) ) ) ( { ctx : this . config . ctx } ) ;
148137 }
149- return config . indexContent || '' ;
138+ return this . config . indexContent || '' ;
150139}
140+
141+ module . exports = Writter ;
0 commit comments