swatch: look for song titles in code metadata

This commit is contained in:
Roipoussiere 2023-06-05 19:04:56 +02:00
parent 11733d5e05
commit 3db1c7a521

View File

@ -1,9 +1,26 @@
export function getMetadata(raw_code) {
// https://stackoverflow.com/a/15123777
const comment_regexp = /\/\*([\s\S]*?)\*\/|([^\\:]|^)\/\/(.*)$/gm;
const tag_regexp = /@([a-z]*):? (.*)/gm
const tags = {};
for (const match of raw_code.matchAll(comment_regexp)) {
const comment = match[1] ? match[1] : '' + match[3] ? match[3] : '';
for (const tag_match of comment.trim().matchAll(tag_regexp)) {
tags[tag_match[1]] = tag_match[2].trim()
}
}
return tags;
}
export function getMyPatterns() {
const my = import.meta.glob('../../../../my-patterns/**', { as: 'raw', eager: true });
return Object.fromEntries(
Object.entries(my) //
.filter(([name]) => name.endsWith('.txt')) //
.map(([name, raw]) => [name.split('/').slice(-1), raw]), //
Object.entries(my)
.filter(([name]) => name.endsWith('.txt'))
.map(([name, raw]) => [ getMetadata(raw)['title'] || name.split('/').slice(-1), raw ]),
);
}