@ -6,11 +6,13 @@ import {pageFormats} from "./formats";
import { PDFFormat } from "puppeteer" ;
import { PDFFormat } from "puppeteer" ;
import { getMarkdownPlugin } from "./utils" ;
import { getMarkdownPlugin } from "./utils" ;
import { logger } from "./logger" ;
import { logger } from "./logger" ;
import { globalVariables } from "./globvars" ;
export class CommandPars er {
export class PreFormatt er {
public projectFiles : string [ ] ;
public projectFiles : string [ ] ;
public pageFormat : PDFFormat ;
public pageFormat : PDFFormat ;
public stylesheets : string [ ] ;
public stylesheets : string [ ] ;
public variables : any ;
private readonly resolvePath : { path : string , lines : number } [ ] ;
private readonly resolvePath : { path : string , lines : number } [ ] ;
@ -18,6 +20,7 @@ export class CommandParser {
this . projectFiles = [ ] ;
this . projectFiles = [ ] ;
this . resolvePath = [ ] ;
this . resolvePath = [ ] ;
this . stylesheets = [ ] ;
this . stylesheets = [ ] ;
this . variables = Object . assign ( { } , globalVariables ) ;
}
}
async processCommands ( doc : string , docpath : string , renderer : Renderer ) {
async processCommands ( doc : string , docpath : string , renderer : Renderer ) {
@ -44,25 +47,26 @@ export class CommandParser {
currentFile = this . resolvePath [ this . resolvePath . length - 1 ] ;
currentFile = this . resolvePath [ this . resolvePath . length - 1 ] ;
}
}
}
}
let match : RegExpExecArray = / \ [ * ! ( \ w + ) * \ ] : ? * ( . * ) / g u . e x e c ( i n p u t L i n e . r e p l a c e ( / \ s / , ' ' ) ) ;
let commandMatch : RegExpExecArray = /\[ *!(\w+) *\]:? *(.*)/g . exec ( inputLine ) ;
let variableMatch : RegExpExecArray = /\$(\w+) *= *(.*?) *$/g . exec ( inputLine ) ;
if ( match && match[ 0 ] ) { // TODO: stylesheets
if ( co mmandM atch && com mandMa tch[ 0 ] ) {
switch ( match[ 1 ] ) {
switch ( co mmandM atch[ 1 ] ) {
case 'use' :
case 'use' :
let plugins = match[ 2 ] . split ( ',' ) ;
let plugins = co mmandM atch[ 2 ] . split ( ',' ) ;
logger . verbose ( ` Adding plugins: ${ match[ 2 ] } ` ) ;
logger . verbose ( ` Adding plugins: ${ co mmandM atch[ 2 ] } ` ) ;
for ( let mdPlugin of plugins ) {
for ( let mdPlugin of plugins ) {
renderer . addPlugin ( getMarkdownPlugin ( mdPlugin . replace ( /^ *| *$/g , '' ) ) ) ;
renderer . addPlugin ( getMarkdownPlugin ( mdPlugin . replace ( /^ *| *$/g , '' ) ) ) ;
}
}
break ;
break ;
case 'include' :
case 'include' :
try {
try {
if ( ! this . resolvePath . find ( x = > x . path === match[ 2 ] ) ) { // if the include is in the path, it is a circular reference
if ( ! this . resolvePath . find ( x = > x . path === co mmandM atch[ 2 ] ) ) { // if the include is in the path, it is a circular reference
let included = await this . getInclude ( match[ 2 ] , mainDir ) ;
let included = await this . getInclude ( co mmandM atch[ 2 ] , mainDir ) ;
inputLines . unshift ( . . . included ) ;
inputLines . unshift ( . . . included ) ;
this . resolvePath . push ( { path : match[ 2 ] , lines : included.length } ) ;
this . resolvePath . push ( { path : co mmandM atch[ 2 ] , lines : included.length } ) ;
} else {
} else {
logger . warning ( ` Circular reference detected. Skipping include ${ match[ 2 ] } ` ) ;
logger . warning ( ` Circular reference detected. Skipping include ${ co mmandM atch[ 2 ] } ` ) ;
}
}
} catch ( err ) {
} catch ( err ) {
logger . error ( err . message ) ;
logger . error ( err . message ) ;
@ -70,28 +74,40 @@ export class CommandParser {
}
}
break ;
break ;
case 'format' :
case 'format' :
if ( ! this . pageFormat && Object . values ( pageFormats ) . includes ( match[ 2 ] ) )
if ( ! this . pageFormat && Object . values ( pageFormats ) . includes ( co mmandM atch[ 2 ] ) )
// @ts-ignore
// @ts-ignore
this . pageFormat = match[ 2 ] ;
this . pageFormat = co mmandM atch[ 2 ] ;
else
else
logger . warning ( 'Invalid page format or format already set: ' + match[ 2 ] ) ;
logger . warning ( 'Invalid page format or format already set: ' + co mmandM atch[ 2 ] ) ;
break ;
break ;
case 'newpage' :
case 'newpage' :
renderer . addPlugin ( getMarkdownPlugin ( markdownPlugins . div ) ) ;
renderer . addPlugin ( getMarkdownPlugin ( markdownPlugins . div ) ) ;
outputLines . push ( '::: .newpage \n:::' ) ;
outputLines . push ( '::: .newpage \n:::' ) ;
break ;
break ;
case 'stylesheet' :
case 'stylesheet' :
await this . addStylesheet ( match[ 2 ] , mainDir ) ;
await this . addStylesheet ( co mmandM atch[ 2 ] , mainDir ) ;
break ;
break ;
default :
default :
outputLines . push ( inputLine ) ;
outputLines . push ( inputLine ) ;
}
}
} else if ( variableMatch ) {
this . variables [ variableMatch [ 1 ] ] = variableMatch [ 2 ] ;
logger . debug ( ` Added variable: " ${ variableMatch [ 1 ] } ": " ${ variableMatch [ 2 ] } " ` ) ;
} else {
} else {
outputLines . push ( inputLine ) ;
outputLines . push ( inputLine ) ;
}
}
}
}
return outputLines . join ( '\n' ) ;
let documentContent = outputLines . join ( '\n' ) ;
// replacing all variables with their values.
for ( let [ key , value ] of Object . entries ( this . variables ) ) {
let varReplacer : RegExp = new RegExp ( ` \\ $ ${ key } ` , "gi" ) ;
if ( typeof value === "function" )
value = await value ( ) ;
// @ts-ignore
documentContent = documentContent . replace ( varReplacer , value ) ;
}
return documentContent ;
}
}
/ * *
/ * *