@@ -32,6 +32,81 @@ var paths = {
3232 template : packagePath + '/build/template.hbs.xml'
3333} ;
3434
35+ /**
36+ * Get file list in a directory.
37+ * Similar to `dir /w` in command prompt and `ls` in bash.
38+ * Expect program quit with error if it does not find the directory.
39+ * @param {string } dir - Directory.
40+ * @return {string[] } - An array as a list of filenames.
41+ */
42+ var getFileList = function ( dir ) {
43+ return fs . readdirSync ( dir ) ;
44+ } ;
45+
46+ /**
47+ * Check whether a config filename is in proper format.
48+ * @param {string } filename - Expected format: 'markdown.theme-name.config.json'.
49+ * @return {boolean }
50+ */
51+ var isThemeNameFormat = function ( filename ) {
52+ return / ^ ( m a r k d o w n \. ) \S + ( \. c o n f i g \. j s o n ) $ / . test ( filename ) ;
53+ } ;
54+
55+ /**
56+ * Get theme name from a config filename, assuming the filename is in proper format.
57+ * @param {string } filename.
58+ * @return {string } - Expected format: 'hyphen-lowercase-theme-name'.
59+ */
60+ var getThemeName = function ( filename ) {
61+ var reHead = / ^ ( m a r k d o w n \. ) / ;
62+ var reTail = / ( \. c o n f i g \. j s o n ) $ / ;
63+ return filename . replace ( reHead , '' ) . replace ( reTail , '' ) ;
64+ } ;
65+
66+ /**
67+ * Create filename of a UDL file base on a theme name.
68+ * @param {string } themeName - Expected format: 'hyphen-lowercase-theme-name'.
69+ * @return {string } - E.g. 'markdown.theme-name.udl.xml'.
70+ */
71+ var createUdlFilename = function ( themeName ) {
72+ return 'markdown.' + themeName + '.udl.xml' ;
73+ } ;
74+
75+ /**
76+ * @typedef {Array } fileListData
77+ * @description A list (array) of file object to be rendered by Handlebars {@link render}-ing.
78+ * @property {Array<Files> } - Each object in the array contains information of {@link Files}.
79+ */
80+ /**
81+ * Create {@link fileListData} according to the files in config directory.
82+ * @param {sting } configPath - Path of config directory.
83+ * @return {fileListData } - A series of {@link Files} objects fit for Handlebars {@link render}-ing.
84+ */
85+ var createFileListData = function ( configPath ) {
86+ /* @type {string[] } Create an array of config files (Default in <config/>). */
87+ var configFileList = getFileList ( configPath ) ;
88+ var fileListData = [ ] ;
89+ var filename , themeName , udlFilename ;
90+ // Loop the config files
91+ for ( var i = 0 ; i < configFileList . length ; i ++ ) {
92+ filename = configFileList [ i ] ;
93+ // Expect all filenames are in format of 'markdown.[theme-name].config.json'.
94+ if ( ! ! isThemeNameFormat ( filename ) ) {
95+ themeName = getThemeName ( filename ) ;
96+ udlFilename = createUdlFilename ( themeName ) ;
97+ // Create a {@link Files } object and append it to {@link fileListData } array.
98+ fileListData . push ( {
99+ config : paths . config + '/' + filename ,
100+ udl : paths . udl + '/' + udlFilename ,
101+ themeName : themeName
102+ } ) ;
103+ } else {
104+ // throw error; //todo
105+ }
106+ }
107+ return fileListData ;
108+ } ;
109+
35110/**
36111 * Create a function of Handlebars template.
37112 * @param {string } templatePath - Full path of template file.
@@ -78,20 +153,17 @@ var render = function(files, template) {
78153 } ) ;
79154} ;
80155
81- /**
82- * Create a Files object in format of {@link Files}
83- * @param {string } themeName - In format of dash-lower-case-theme-name
84- * @param {Paths } paths - The {@link Paths} object for Handlebars rendering
85- * @return {Files }
86- */
87- var createFilesObj = function ( themeName , paths ) {
88- return {
89- config : paths . config + '/markdown.' + themeName + '.config.json' ,
90- udl : paths . udl + '/markdown.' + themeName + '.udl.xml' ,
91- themeName : themeName
92- } ;
156+ var main = function ( fileListData , templatePath ) {
157+ /* @function template - Create a template function required by Handlebars. */
158+ var template = createTemplate ( templatePath ) ;
159+ // Put the {@link Files } objects to {@link render } using the {@link template } above.
160+ for ( var i = 0 ; i < fileListData . length ; i ++ ) {
161+ console . log ( fileListData [ i ] ) ;
162+ // Render the files asynchronously.
163+ // render(fileListData[i], template);
164+ }
93165} ;
166+ // module.exports = function(fileListData, templatePath) {}; //todo
94167
95- var template = createTemplate ( paths . template ) ;
96- var defaultFiles = createFilesObj ( 'default' , paths ) ;
97- render ( defaultFiles , template ) ;
168+ var fileListData = createFileListData ( paths . config ) ;
169+ main ( fileListData , paths . template ) ;
0 commit comments