Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
clean up parse script
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Oct 7, 2019
1 parent 80a7910 commit c33d4d0
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Parses README.md into a JSON data structure for use in other projects.
resources: [
{
"name": "`@sveltejs/svelte-virtual-list`",
"repo": "https://github.com/sveltejs/svelte-virtual-list",
"url": "https://github.com/sveltejs/svelte-virtual-list",
"description": "A virtual list component for Svelte apps",
"tags": [
"components and libraries",
Expand All @@ -35,9 +35,8 @@ The motivating use case is to deprecate `awesome-svelte-resources`
in favor of an official Svelte resources website.
https://github.com/ryanatkn/awesome-svelte-resources/issues/18
TODO consider these changes
- add an npm field if name is in `backticks`
- change "repo" field to "url" if `<sub>` isn't detected
Additional parsing and transformation will be done downstream -
the purpose of this script is to just get everything in a programmable form.
*/

Expand All @@ -49,17 +48,18 @@ const inputPath = args[1] || 'README.md';

const omittedTags = ['contents', 'license'];

const BEGIN_TAG_MATCHER = /^(#+) (.+)/;
const BEGIN_RESOURCE_MATCHER = /^\W*-/;

const parseReadme = readme => {
const lines = readme.split('\n');

// result vars
const resources = [];
const tags = [];

// parsing state vars
const currentTags = [];
let skipThisTag = false;

// open a tag at some depth, first closing any tags at equal or greater depth
// depth is 0 for top-level tags
const beginTag = (tag, depth) => {
tags.push(tag);
Expand All @@ -73,7 +73,7 @@ const parseReadme = readme => {
const line = lines[i];

// begin tag
const tagMatches = line.match(BEGIN_TAG_MATCHER);
const tagMatches = line.match(/^(#+) (.+)/);
if (tagMatches) {
const tag = tagMatches[2];
const depth = tagMatches[1].length - 2; // `- 2` because `##` is the top level
Expand All @@ -83,19 +83,14 @@ const parseReadme = readme => {
continue;
}

if (skipThisTag) continue;
if (skipThisTag) continue; // line is a skipped tag

// resource
const isResource = BEGIN_RESOURCE_MATCHER.test(line);
if (isResource) {
if (isResource(line)) {
const resourceLines = [line];
// resources are only 1 or 2 lines right now, so this only handles that case
const nextLine = lines[i + 1];
if (
nextLine &&
nextLine.startsWith(' ') &&
!BEGIN_RESOURCE_MATCHER.test(nextLine)
) {
if (nextLine && nextLine.startsWith(' ') && !isResource(nextLine)) {
resourceLines.push(nextLine);
i++;
}
Expand All @@ -107,32 +102,32 @@ const parseReadme = readme => {
return {tags, resources};
};

const isResource = line => /^\W*-/.test(line);

const createResource = (lines, currentTags) => {
const [line1, line2] = lines;
if (lines[2]) {
if (lines.length > 2) {
throw Error(`Resources are expected to have max 2 lines - ${line1}`);
}
const matches = line1.match(/\[(.+)\]\((.+)\)/);
const description = line2 && line2.trim();
const [line1, line2] = lines;
const [_, name, url] = line1.match(/\[(.+)\]\((.+)\)/);
return {
name: matches[1],
repo: matches[2],
description,
name,
url,
description: line2 && line2.trim(),
tags: currentTags.slice(),
};
};

const transform = parsed => parsed;

const main = () => {
const readme = fs.readFileSync(inputPath, 'utf8');
const parsed = parseReadme(readme);
const data = transform(parsed);
console.log(`parsed ${data.resources.length} resources`);
const outputData = JSON.stringify(data, null, 2);
fs.writeFileSync(outputPath, outputData, 'utf8');
console.log(
`parsed ${parsed.resources.length} resources and ${parsed.tags.length} tags`,
);
const outputString = JSON.stringify(parsed, null, 2);
fs.writeFileSync(outputPath, outputString, 'utf8');
console.log(`wrote to ${outputPath}`);
console.log('all done');
console.log('all done :)');
};

main();

0 comments on commit c33d4d0

Please sign in to comment.