Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto update sphinx theme css files #291

Open
wants to merge 1 commit into
base: source
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions website/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(node website/updateSiteConfig.js && git add website/siteConfig.js) || true
11 changes: 8 additions & 3 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
"scripts": {
"examples": "docusaurus-examples",
"start": "docusaurus-start",
"build": "docusaurus-build",
"fix-css": "sed -ibak -e 's/@supports selector(.*11\\.5rem)}}//' ./static/docs/current/_static/sphinx_immaterial_theme.*.css",
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems we now have two ways of fixing the CSS - one with the nodejs script you added, and another being this way using sed. I would prefer to unify it

Copy link
Author

Choose a reason for hiding this comment

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

@ZacBlanco This PR includes 2 different fixes:

  1. Updated files matching the pattern sphinx_immaterial_theme*.css in siteConfig.js using a Node.js script updateSiteConfig.js.
  2. Removed unsupported CSS syntax using sed ( from the fix-css in package.json)

And one enhancement:

  • Integrated Husky to add Git pre-commit hooks for updating siteConfig.js.

The goal of this PR is to automate the manual steps involved in releasing documentation, enabling the integration with GitHub Actions in future, and simplifying local testing with minimal manual effort.

"build-site": "docusaurus-build",
"build": "yarn fix-css; yarn build-site",
"publish-gh-pages": "docusaurus-publish",
"write-translations": "docusaurus-write-translations",
"version": "docusaurus-version",
"rename-version": "docusaurus-rename-version"
"rename-version": "docusaurus-rename-version",
"update-site-config": "updateSiteConfig",
"prepare": "cd .. && husky website/.husky"
},
"devDependencies": {
"docusaurus": "^1.7.1"
"docusaurus": "^1.7.1",
"husky": "^9.1.7"
}
}
68 changes: 68 additions & 0 deletions website/updateSiteConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const fs = require('fs');
const path = require('path');

async function updateSphinxThemeFiles() {
const siteConfigPath = path.join(__dirname, './siteConfig.js');
const staticDocsPath = path.join(__dirname, 'static/docs');

let content = fs.readFileSync(siteConfigPath, 'utf8');
let lines = content.split('\n');

const pattern = /\Wstatic\/sphinx_immaterial_theme\.[a-f0-9]+\.min\.css\W/;
const insertLineIndex = lines.findIndex(line => pattern.test(line));

if (insertLineIndex === -1) {
console.error('Could not find sphinx theme file references in siteConfig.js');
process.exit(1);
}

matchSet = new Set();
lines = lines.filter(line => {
if ( !pattern.test(line) ){
return true;
} else {
matchSet.add(line.trim());
return false;
}
});

const versionFolders = fs.readdirSync(staticDocsPath)
.filter(folder => /^\d+\.\d+(\.\d+)?$/.test(folder));

if (versionFolders.length === 0) {
console.error('No version folders found');
process.exit(1);
}

const allCssFiles = [];
const fileSet = new Set();
for (const version of versionFolders) {
const staticPath = path.join(staticDocsPath, version, '_static');
if (fs.existsSync(staticPath)) {
const files = fs.readdirSync(staticPath)
.sort()
.filter(file => file.startsWith('sphinx_immaterial_theme.') && file.endsWith('.min.css'))
.filter(file => {
if ( fileSet.has(file) ) return false;
fileSet.add(file);
return true;
})
.map(file => ` "static/${file}",`);
allCssFiles.push(...files);
}
}

if (matchSet.size === allCssFiles.length &&
allCssFiles.every(file => matchSet.has(file.trim()))) {
process.exit(1);
}

console.info('Found CSS files:', allCssFiles);
lines.splice(insertLineIndex, 0, ...allCssFiles);

fs.writeFileSync(siteConfigPath, lines.join('\n'));
console.log('Successfully updated sphinx theme file references');
process.exit(0);
}

updateSphinxThemeFiles().catch(console.error);