Proper parsing of full description
This commit is contained in:
parent
a588432516
commit
62f0bfbf25
12
src/index.ts
12
src/index.ts
|
|
@ -1,7 +1,5 @@
|
|||
import * as fs from "fs";
|
||||
import { parseRule } from "./parser";
|
||||
|
||||
const isDefined = <T>(x: T | undefined): x is T => Boolean(x);
|
||||
import { parseFile } from "./parser";
|
||||
|
||||
const filename = process.argv[2];
|
||||
|
||||
|
|
@ -9,13 +7,9 @@ console.log(`Parsing: ${filename}`);
|
|||
|
||||
const file = fs.readFileSync(filename, "utf8");
|
||||
|
||||
file
|
||||
.split(/\n/)
|
||||
.map(parseRule)
|
||||
.filter(isDefined)
|
||||
.forEach((rule) => {
|
||||
parseFile(file).forEach((rule) => {
|
||||
console.log(rule.type);
|
||||
rule.params.forEach((p) => {
|
||||
console.log(` ${p.type}: ${p.value}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -30,8 +30,9 @@ const parseParams = (type: RuleType, text: string): Param[] => {
|
|||
switch (type) {
|
||||
case RuleType.Name:
|
||||
case RuleType.Author:
|
||||
case RuleType.Description:
|
||||
case RuleType.Description: {
|
||||
return [{ type: ParamType.Text, value: text }];
|
||||
}
|
||||
case RuleType.Warmup:
|
||||
case RuleType.Rest:
|
||||
case RuleType.Interval:
|
||||
|
|
@ -40,7 +41,7 @@ const parseParams = (type: RuleType, text: string): Param[] => {
|
|||
}
|
||||
};
|
||||
|
||||
export const parseRule = (line: string): Rule | undefined => {
|
||||
const parseRule = (line: string): Rule | undefined => {
|
||||
const matches = line.match(/^(\w+):(.*)$/);
|
||||
if (!matches) {
|
||||
return undefined;
|
||||
|
|
@ -55,3 +56,21 @@ export const parseRule = (line: string): Rule | undefined => {
|
|||
params: parseParams(type, matches[2].trim()),
|
||||
};
|
||||
};
|
||||
|
||||
export const parseFile = (file: string): Rule[] => {
|
||||
const rules: Rule[] = [];
|
||||
|
||||
file.split("\n").forEach((line) => {
|
||||
const rule = parseRule(line);
|
||||
if (rule) {
|
||||
rules.push(rule);
|
||||
return;
|
||||
}
|
||||
const lastRule = rules[rules.length - 1];
|
||||
if (lastRule && lastRule.type === RuleType.Description) {
|
||||
lastRule.params.push({ type: ParamType.Text, value: line.trim() });
|
||||
}
|
||||
});
|
||||
|
||||
return rules;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue