Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const nunjucks = require('nunjucks');

const ATTRIB_INCLUDE_PATH = 'include-path';
const ATTRIB_CWF = 'cwf';

const BOILERPLATE_DIRECTORY_NAME = '_boilerplates';
/*
* Utils
*/
Expand Down Expand Up @@ -51,6 +53,11 @@ Parser.prototype._preprocess = function (element, context, config) {
element.name = isInline ? 'span' : 'div';
element.attribs[ATTRIB_INCLUDE_PATH] = filePath;

let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath);
if (!isUrl && !fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) {
filePath = boilerplateFilePath;
}

if (isDynamic) {
element.name = 'dynamic-panel';
element.attribs.src = filePath;
Expand Down Expand Up @@ -137,6 +144,10 @@ Parser.prototype._preprocess = function (element, context, config) {
element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
}
filePath = path.resolve(path.dirname(context.cwf), includeSrcPath); // updated path (no hash)
let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath);
if (!fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) {
filePath = boilerplateFilePath;
}
}
element.attribs.src = filePath;
this.dynamicIncludeSrc.push({from: context.cwf, to: filePath});
Expand All @@ -153,6 +164,10 @@ Parser.prototype._preprocess = function (element, context, config) {
element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
}
filePath = path.resolve(path.dirname(context.cwf), includeSrcPath); // updated path (no hash)
let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath);
if (!fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) {
filePath = boilerplateFilePath;
}
}
element.attribs.src = filePath;
this.dynamicIncludeSrc.push({from: context.cwf, to: filePath});
Expand Down Expand Up @@ -195,7 +210,7 @@ Parser.prototype._parse = function (element, context) {
element.attribs.isOpen = hasIsOpen ? hasIsOpen : false;
try {
if (fs.statSync(element.attribs.src).isFile()) {
let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src)));
let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change necessary?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break when hosted on GitHub:

screenshot 2018-01-27 02 16 11

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a dynamic panel includes a boilerplate file,
I will replace the filePath and element.attribs.src with the boilerplate filepath. However, it does not work correctly as it is unable to retrieve the content from source: /\_boilerplates\filename._include_.html.

I tried normalizing the filepath and replacing it with the above line. It works locally during testing but I didn't know it will break when hosted on Github.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The {{baseUrl}} is /websitehttps://nus-cs2103-ay1718s2.github.io/website/

Doing ../schedule will give https://nus-cs2103-ay1718s2.github.io/schedule/
It's important that rendered files do not contain relative paths for include to work well.

element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html'));
if (element.attribs.fragment) {
element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path
Expand All @@ -215,7 +230,7 @@ Parser.prototype._parse = function (element, context) {
}
try {
if (fs.statSync(element.attribs.src).isFile()) {
let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src)));
let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src));
element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html'));
if (element.attribs.fragment) {
element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path
Expand All @@ -232,7 +247,7 @@ Parser.prototype._parse = function (element, context) {
}
try {
if (fs.statSync(element.attribs.src).isFile()) {
let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src)));
let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src));
element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html'));
if (element.attribs.fragment) {
element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path
Expand Down