Skip to content

Commit

Permalink
Refactor grammar building
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelong committed Mar 30, 2024
1 parent e043d95 commit c787db9
Show file tree
Hide file tree
Showing 12 changed files with 3,891 additions and 3,854 deletions.
4 changes: 2 additions & 2 deletions build/all.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bailout = require('./generate-bailout')
const md = require('./latex-md')
const codeblocks = require('./generate-grammar-blocks')
const buildLatexGrammars = require('./generate-latex-grammars')

const arg = process.argv[2]
if (arg === undefined || arg === 'build-latex') {
/* Make sure to generate grammar blocks first */
codeblocks()
buildLatexGrammars()
}
if (arg === 'update-external' || arg === undefined) {
bailout()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')


const mintedEnvs = ['minted', 'lstlisting', 'pyglist']
const robustExternalizeEnvs = ['CacheMeCode', 'PlaceholderPathFromCode\\*?', 'PlaceholderFromCode\\*?', 'SetPlaceholderCode\\*?']
Expand Down Expand Up @@ -42,6 +44,19 @@ const codeLanguages = [
{name: ['sympycode', 'sympyverbatim', 'sympyblock', 'sympyconcode', 'sympyconsole', 'sympyconverbatim'], source: 'source.python'},
]

/**
* Convert an input yaml file to a json output file
* @param {string} inputfile a yaml file name
* @param {string} outputfile a json file name
*/
function convertYamlToJson(inputfile, outputfile) {
try {
const grammar = yaml.load(fs.readFileSync(inputfile, {encoding: 'utf-8'}))
fs.writeFileSync(outputfile, JSON.stringify(grammar, undefined, 4))
} catch (error) {
console.log(error)
}
}

/**
* Indent text and replace spaces indentation with tabs
Expand Down Expand Up @@ -218,16 +233,26 @@ function generateRobustExternalizeBlock(envNames, language, source, contentName=
}

function main() {
console.log('Generating LaTeX.tmLanguage from data/')
const syntaxesDir = path.join(__dirname, '..', 'syntaxes')
const syntaxesSrcDir = path.join(syntaxesDir, 'src')

console.log('Generating BibTeX.tmLanguage from src/')
convertYamlToJson(path.join(syntaxesSrcDir, 'BibteX.tmLanguage.yaml'), path.join(syntaxesDir, 'BibteX.tmLanguage.json'))

console.log('Generating TeX.tmLanguage from src/')
convertYamlToJson(path.join(syntaxesSrcDir, 'TeX.tmLanguage.yaml'), path.join(syntaxesDir, 'TeX.tmLanguage.json'))

var mintedDefinitions = mintedLanguages.map(language => generateMintedBlock(mintedEnvs, language.language, language.source, language?.contentName)).join(',\n')
var codeDefinitions = codeLanguages.map(language => generateCodeBlock(language.name, language.source, language?.contentName)).join(',\n')
var robustExternalizeDefinitions = robustExternalizeLanguages.map(language => generateRobustExternalizeBlock(robustExternalizeEnvs, language.language, language.source, language?.contentName)).join(',\n')

let text = fs.readFileSync(path.join(__dirname, '..', 'syntaxes', 'data', 'LaTeX.tmLanguage.json'), {encoding: 'utf8'})
text = text.replace(/^\s*\{"includeMintedblocks": ""\}/gm, indent(4, mintedDefinitions))
text = text.replace(/^\s*\{"includeRobustExternalizeBlocks": ""\}/gm, indent(4, robustExternalizeDefinitions))
text = text.replace(/^\s*\{"includeCodeBlocks": ""\}/gm, indent(2, codeDefinitions))
fs.writeFileSync(path.join(__dirname, '..', 'syntaxes', 'LaTeX.tmLanguage.json'), text)
console.log('Generating LaTeX.tmLanguage from src/')
convertYamlToJson(path.join(syntaxesSrcDir, 'LaTeX.tmLanguage.base.yaml'), path.join(syntaxesSrcDir, 'LaTeX.tmLanguage.json'))
let text = fs.readFileSync(path.join(syntaxesSrcDir, 'LaTeX.tmLanguage.json'), {encoding: 'utf8'})
text = text.replace(/^\s*\{(\n\s*)?"includeMintedblocks": ""(\n\s*)?\}/gm, indent(4, mintedDefinitions))
text = text.replace(/^\s*\{(\n\s*)?"includeRobustExternalizeBlocks": ""(\n\s*)?\}/gm, indent(4, robustExternalizeDefinitions))
text = text.replace(/^\s*\{(\n\s*)?"includeCodeBlocks": ""(\n\s*)?\}/gm, indent(2, codeDefinitions))
fs.writeFileSync(path.join(syntaxesDir, 'LaTeX.tmLanguage.json'), text)
}


Expand Down
5 changes: 5 additions & 0 deletions build/latex-md.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const fs = require('fs')
const vel = require('vscode-extend-language')

/*
* Generate a combined LaTeX Markdown grammar to be sued in LaTeX document
* using the markdown package.
*/

async function insertLaTeXGrammar(url, latexScope, newScopeName, newGrammarFile) {
const grammar = JSON.parse(await vel.download(url))
if(!grammar) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"got": "12.5.2",
"mocha": "10.2.0",
"textmate-bailout": "1.1.0",
"vscode-extend-language": "^0.1.1"
"vscode-extend-language": "^0.1.1",
"js-yaml":"^4.1.0"
}
}
Loading

0 comments on commit c787db9

Please sign in to comment.