import { parseTokens } from './tokenizer'; import { Section } from './types/Section'; import { processScript } from './parsers/script'; import { processHTML } from './parsers/html'; import { processStyle } from './parsers/css'; import { parseSection } from './parsers/section'; import fs from 'fs'; import { render } from './renderer'; import { CodeSection } from './types/CodeSection'; import { interpret } from './interpretation'; const SPACE_INDENTS = 4; export function run( outline: string ) { const sections = extractSections( outline ); let levels = sections.map( section => extractLevels( section ) ); const sectionSymbolGroups = levels.map( group => translateLevelGroupToSymbols( group ) ); const sectionTypes = sectionSymbolGroups.map( section => parseSection( section ) ); const intermediate: CodeSection[] = sectionTypes.map( (section: Section | null) => generateCode( section as Section ) ) as CodeSection[]; const sorted = intermediate.sort( ( a: CodeSection, b: CodeSection ) => { if( a.type === 'script' ) return -1; return 0; } ); //fs.writeFileSync('./out.json', JSON.stringify(intermediate, null, 4)); return render( interpret( sorted ) ); } /** * Processes a complete body (or outline) of code into sections which are processed individually */ function extractSections( outline: string ) { const lines = outline.split( '\n' ); let sections = []; let inSection = false; let entryIndentation = -1; for( let i=0; i < lines.length; i++ ) { const line = lines[i]; if( line.trim() === '' ) { inSection = false; continue; } const indentation = getMinIndentationLevel( line ); if( inSection ) { if( indentation >= entryIndentation ) { sections[sections.length - 1] += `\n${line}`; } else { inSection = false; } } else { sections.push( line ); inSection = true; entryIndentation = indentation; } } return sections; } /** * Processes an individual section of the document into an array of lines with potential recursive sub-arrays, essentially outlining the code's indentation */ function extractLevels ( section: string ): any { const currentIndent = getMinIndentationLevel( section ); const lines = section.split( '\n' ); let groups = []; for( let i=0; i < lines.length; i++ ) { const line = lines[i]; const indent = getMinIndentationLevel( line ); if( indent === currentIndent ) { groups.push( line ); } else if( indent === currentIndent + 1 ) { let subContent = ''; for( let l=i; l < lines.length; l++ ) { const subLine = lines[l]; const subIndent = getMinIndentationLevel( subLine ); if( subIndent >= indent ) { subContent += subLine + '\n'; i++; } else { break; } } i--; subContent = subContent.trimRight(); groups.push( extractLevels( subContent ) ); } } return groups; } function translateLevelGroupToSymbols( group: string[] ): any { return group.map( (item: string) => { if( typeof(item) === 'string' ) { return parseTokens( item ); } else { return translateLevelGroupToSymbols( item ); } }); } function generateCode( section: Section ) { switch( section.type ) { case 'css': return processStyle( section.lines ); case 'html': return processHTML( section.lines ); case 'script': return processScript( section.lines ); } return ''; } /** * Returns the base indentation level of a body of text */ export function getMinIndentationLevel( body: string ): number { const lines = body.split( '\n' ); let minIndents = 99999; for( let i=0; i < lines.length; i++ ) { const chars = lines[i].split( '' ); let spaceCount = 0; for( let l=0; i < chars.length; l++ ) { if( chars[l] !== ' ' ) break; spaceCount++; } const indentCount = Math.floor( spaceCount / SPACE_INDENTS ); if( indentCount < minIndents ) { minIndents = indentCount; } } return minIndents; } //fs.writeFileSync('./out.html', run(fs.readFileSync('./example.txt').toString()));