Comments (2)
In order to demonstrate my intentions a bit clearer. This is the current post-processing script I created
to achieve the same result
Javascript (nodejs)
async function rewriteLanguagePaths(directory, defaultLanguage = 'en') {
// Get index files in the directory
const indexFiles = fs.readdirSync(directory)
.filter(file => file.startsWith("index"));
indexFiles.forEach(index => {
// Language of the index file
const language = /index-(.*?).html/g.exec(index)[1];
const oldFilePath = path.join(directory, index);
const newFilePath = path.join(directory, language, "index.html");
// Create language directory
fs.mkdirSync(path.join(directory, language));
// Copy the file to /${language}/index.html
fs.copyFileSync(oldFilePath, newFilePath);
// URL rewriting in the new directory
let contents = fs.readFileSync(newFilePath, { encoding: 'utf-8' });
contents = contents.replace(/href="index-([a-z]{2}?).html"/g, 'href="$1/"');
contents = contents.replace(/(href|src?)=\"((?!http).*?)\"/g, '$1="../$2"');
contents = contents.replace(/.load\(\"((?!http).*?)\"/g, '.load\("../$1"');
fs.writeFileSync(newFilePath, contents);
if (defaultLanguage !== language) {
// Remove index file if not the default language
fs.rmSync(oldFilePath);
} else {
// Rename the index-*.html file to index.html
const defaultIndex = path.join(directory, "index.html");
fs.renameSync(oldFilePath, defaultIndex);
let contents = fs.readFileSync(defaultIndex, { encoding: 'utf-8' });
contents = contents.replace(/href="index-([a-z]{2}?).html"/g, 'href="$1/"');
fs.writeFileSync(defaultIndex, contents);
}
});
}
This is a slightly more complex example that fixes the webvowl iframe issue in a section (not an ideal solution)
async function rewriteLanguagePaths(directory, defaultLanguage = 'en') {
// Replacement patterns
const relativePathPattern = /(href|src?)=\"((?!http|#).*?)\"/g;
const relativePathSourcePattern = /.load\(\"((?!http).*?)\"/g;
const languagePathPattern = /href="index-([a-z]{2}?).html"/g;
// Get section files in the sections directory
const sectionFiles = fs.readdirSync(path.join(directory, "sections"));
// Create a mapping for section files with relative URIs
const sectionMapping = sectionFiles.map(file => {
const filePath = path.join(directory, "sections", file);
let contents = fs.readFileSync(filePath, { encoding: 'utf-8' });
if (relativePathPattern.exec(contents)) {
contents = contents.replace(relativePathPattern, '$1="../$2"');
const newFile = file.replace(".html", "_.html");
fs.writeFileSync(path.join(directory, "sections", newFile), contents);
return { [file]: newFile }
} else {
return { [file]: file };
}
}).reduce((a, b) => ({...a, ...b}), {});
// Get index files in the directory
const indexFiles = fs.readdirSync(directory)
.filter(file => file.startsWith("index"));
indexFiles.forEach(index => {
// Language of the index file
const language = /index-(.*?).html/g.exec(index)[1];
const oldFilePath = path.join(directory, index);
const newFilePath = path.join(directory, language, "index.html");
// Create language directory
fs.mkdirSync(path.join(directory, language));
// Copy the file to /${language}/index.html
fs.copyFileSync(oldFilePath, newFilePath);
// URL rewriting in the new directory
let contents = fs.readFileSync(newFilePath, { encoding: 'utf-8' });
contents = contents.replace(languagePathPattern, 'href="$1/"');
contents = contents.replace(relativePathPattern, '$1="../$2"');
contents = contents.replace(relativePathSourcePattern, '.load\("../$1"');
// Replace sections with relative href or src
Object.keys(sectionMapping).forEach(sectionKey => {
contents = contents.replace(sectionKey, sectionMapping[sectionKey]);
});
fs.writeFileSync(newFilePath, contents);
if (defaultLanguage !== language) {
// Remove index file if not the default language
fs.rmSync(oldFilePath);
} else {
// Rename the index-*.html file to index.html
const defaultIndex = path.join(directory, "index.html");
fs.renameSync(oldFilePath, defaultIndex);
let contents = fs.readFileSync(defaultIndex, { encoding: 'utf-8' });
contents = contents.replace(languagePathPattern, 'href="$1/"');
fs.writeFileSync(defaultIndex, contents);
}
});
}
from widoco.
This is a nice suggestion, as the whole hierarchy remains cleaner. I am thinking that this should become the default behavior, but it may cause some issues with existing tools. Probably I did not implement this due to convenience. It may be worth exploring.
from widoco.
Related Issues (20)
- Evaluation section hidden
- Ability to retain indent and newlines in literal values HOT 4
- skos:note and subclasses HOT 7
- Including imported annotation properties in new ontology HOT 1
- dcterms:title not in generated documentation HOT 1
- Unable to generate documentat for the ontology adms.rdf HOT 3
- Unable to generate links in the annotation property rdfs:comment HOT 2
- Encoding issue HOT 1
- Subfolder and page generation for instances HOT 1
- Properties for generating introduction and description sections HOT 9
- Properly format xsd:anyURI label (CamelCase) HOT 1
- Question: Best way to load custom introduction/description via CLI HOT 1
- Wrong label if multiple labels are provided HOT 1
- Bad zip entry HOT 4
- Take modular ontologies as input and generate modular sections in HTML
- Can Widoco generate a HTML page per term?
- Add Security check for dependencies. HOT 2
- Mac OSX HOT 1
- Include owl:equivalentClass in the documentation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from widoco.