Skip to content

Commit

Permalink
Add CDL toggle function, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGautier committed Sep 20, 2024
1 parent dfa2d84 commit b390d9d
Show file tree
Hide file tree
Showing 6 changed files with 1,378 additions and 8,614 deletions.
52 changes: 40 additions & 12 deletions lib/cdlDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ function processImg ($) {
relPath = relPath.replace(new RegExp('^' + libName), libPath.nameOnDisk)
}
const newImgName = relPath.replace(/\//g, '_')
paths = { ...paths, [newImgName]: path.join(libPath.path, relPath) }
const alt = $(this).attr('alt')
$(this).replaceWith(`<img alt="${alt}" src="${path.join('img', newImgName)}">`)
paths = { ...paths, [newImgName]: path.join(libPath.path, relPath) };
$(this).attr('src', path.join('img', newImgName));
})
return paths
}
Expand All @@ -248,7 +247,7 @@ function processImg ($) {
*
* - If the href attribute points to a Modelica URI, it modifies the href to point
* to the corresponding section within the documentation.
* - If the href does not * point to another section, it removes the href attribute
* - If the href does not point to another section, it removes the href attribute
* and wraps the anchor tag in a span with the resource path as ID attribute.
*
* @param {Object} $ - The jQuery-like object for manipulating the HTML content.
Expand All @@ -274,11 +273,35 @@ function processHref ($, documentation) {
// If the href attribute doesn't point to another section of the documentation, remove
$(this)
.removeAttr('href')
.wrap(`<span style="color: red;" id="${href}"></span>`);
.wrap(`<span style="color: grey;" id="${href}"></span>`);
}
})
}

/**
* Processes CDL visibility toggles.
*
* - Modifies the cheerio object inplace.
* - If the visible attribute evaluates to false, the span element is emptied.
* Syntax: <span><!-- cdl(visible=...) -->...<!-- end cdl --></span>
*
* @param {Object} $ - The cheerio object.
* @param {Object} evalContext - The evaluation context for expressions.
* @param {string} instanceName - The name of the instance for which the expression is evaluated.
*/
function processCdlToggle ($, evalContext, instanceName) {
let $span = $('span:contains(cdl):contains(end cdl)')

$span.contents().map(function() {
if (/<--.*cdl\s*\(.*visible/.test($(this).text())) {
const visibleExp = $(this).text().replace(/<--.*visible=(.*)\).*-->/, '$1')
if (expressionEvaluation.evalExpression(visibleExp, evalContext, instanceName) === false) {
$span.empty()
}
}
})
}

/**
* Generates a new heading number based on the current heading index and
* the previous heading number.
Expand Down Expand Up @@ -370,14 +393,18 @@ function modifyInfo (docElement, evalContext, unitContext, unitData) {
htmlStr = htmlStr.replace(/"?(<\/?html>)"?/g, '$1')
// Parse document: this will create boilerplate tags (<body>, <head>) if not present
const $ = cheerio.load(htmlStr)

// Process CDL visibility toggles
processCdlToggle($, evalContext, docElement.instance?.name)

// Shift index of existing headings
const headings = []
$('h1, h2, h3, h4, h5, h6, h7, h8').map((_, el) =>
$('h1, h2, h3, h4, h5, h6').map((_, el) =>
headings.push(Number(el.name.replace('h', ''))))
if (headings.length > 0) {
const headingOffset = docElement.headingIdx - math.min(headings) + 1;
let headingNum = docElement.headingNum;
$('h1, h2, h3, h4, h5, h6, h7, h8').replaceWith((_, el) => {
$('h1, h2, h3, h4, h5, h6').replaceWith((_, el) => {
const headingIdx = Number(el.name.replace('h', '')) + headingOffset;
headingNum = createHeadingNum(headingIdx, headingNum);
return createHeading(headingIdx, headingNum, $(el).text());
Expand Down Expand Up @@ -408,19 +435,18 @@ function modifyInfo (docElement, evalContext, unitContext, unitData) {
return `${$('body').html()}`
}


/**
* Builds the documentation for a given class object and writes it to an HTML file.
*
* @param {Object} classObj - The class object to document.
* @param {Object} jsons - The JSON data of all used classes.
* @param {Object} unitData - The data containing unit conversion information.
* @param {string} title - The title of the documentation.
* @param {string} outputDir - The directory where the documentation will be saved.
* @param {string} [title] - The title of the documentation.
*
* @returns {void}
*/
function buildDoc (classObj, jsons, unitData, title, outputDir) {
function buildDoc (classObj, jsons, unitData, outputDir, title) {
// First extract parameters and documentation of all components
const paramAndDoc = expressionEvaluation.getParametersAndBindings(
classObj, jsons, /* fetchDoc= */ true);
Expand Down Expand Up @@ -458,12 +484,14 @@ function buildDoc (classObj, jsons, unitData, title, outputDir) {
const destPath = path.join(outputDir, 'img', img);
// Copy image if it doesn't exist
if (!fs.existsSync(destPath)) {
fs.copyFileSync(imgData.paths[img], destPath);
fs.copyFileSync(paths[img], destPath);
}
}

// Write the HTML file to the output directory
fs.writeFileSync(path.join(outputDir, 'documentation.html'), $.html(), 'utf8');
fs.writeFileSync(path.join(
outputDir, `${title ?? 'Sequence of Operation'}.html`
), $.html(), 'utf8');
}

module.exports.buildDoc = buildDoc
Empty file.
1 change: 1 addition & 0 deletions test/expressionEvaluation/MultiZoneVav.json

Large diffs are not rendered by default.

Loading

0 comments on commit b390d9d

Please sign in to comment.