Skip to content

Commit

Permalink
Simplify parsing of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelfangjw committed Jun 16, 2021
1 parent a6139ce commit 9899ce9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 224 deletions.
16 changes: 5 additions & 11 deletions src/features/sicp/parser/ParseJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ const handleContainer = (obj: JsonType, refs: React.MutableRefObject<{}>) => {
);
};

const handleReference = (obj: JsonType, refs: React.MutableRefObject<{}>) => {
return <div>{parseArr(obj['child']!, refs)}</div>;
};

const handleText = (text: string) => {
return <p>{text}</p>;
};
Expand Down Expand Up @@ -221,19 +225,13 @@ export const processingFunctions = {

LaTeX: (_obj: JsonType, _refs: React.MutableRefObject<{}>) => handleText('LaTeX'),

MATTER: handleContainer,

META: (obj: JsonType, _refs: React.MutableRefObject<{}>) => <em>{obj['body']}</em>,

METAPHRASE: (obj: JsonType, _refs: React.MutableRefObject<{}>) => handleText(obj['body']!),

OL: (obj: JsonType, refs: React.MutableRefObject<{}>) => <OL>{parseArr(obj['child']!, refs)}</OL>,

REF: handleRef,

REFERENCE: handleContainer,

REFERENCES: handleContainer,
REFERENCE: handleReference,

SECTION: handleContainer,

Expand All @@ -245,17 +243,13 @@ export const processingFunctions = {
</h2>
),

SUBSECTION: handleContainer,

SUBSUBHEADING: (obj: JsonType, refs: React.MutableRefObject<{}>) => (
<h4 className="bp3-heading" ref={ref => (refs.current[obj['id']!] = ref)}>
<br />
{parseArr(obj['child']!, refs)}
</h4>
),

SUBSUBSECTION: handleContainer,

TABLE: (obj: JsonType, refs: React.MutableRefObject<{}>) => (
<table>
<tbody>{obj['child']!.map((x, index) => handleTR(x, refs, index))}</tbody>
Expand Down
8 changes: 4 additions & 4 deletions src/features/sicp/parser/__tests__/ParseJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { JsonType, parseArr, ParseJsonError, parseObj, processingFunctions } fro

// Tags to process
const headingTags = ['SUBHEADING', 'SUBSUBHEADING'];
const sectionTags = ['SECTION', 'SUBSECTION', 'SUBSUBSECTION', 'MATTER', 'CHAPTER', 'REFERENCES'];
const listTags = ['OL', 'UL'];
const symbolTags = ['BR', 'LaTeX', 'TeX'];
const stylingTags = ['B', 'EM', 'JAVASCRIPTINLINE', 'TT', 'METAPHRASE', 'META'];
const stylingTags = ['B', 'EM', 'JAVASCRIPTINLINE', 'TT', 'META'];
const latexTags = ['LATEX', 'LATEXINLINE'];
const linkTags = ['LINK', 'REF', 'FOOTNOTE_REF'];

const epigraphTag = 'EPIGRAPH';
const tableTag = 'TABLE';
const exerciseTag = 'EXERCISE';
const sectionTag = 'SECTION';
const snippetTag = 'SNIPPET';
const figureTag = 'FIGURE';
const displayFootnoteTag = 'DISPLAYFOOTNOTE';
Expand Down Expand Up @@ -79,12 +79,12 @@ describe('Parse heading', () => {
});

describe('Parse section', () => {
const tags = sectionTags;
const tag = sectionTag;
const text = { tag: 'TEXT', child: [mockData['text'], mockData['text']] };
const content = [text, text];
const obj = { body: 'Title', child: content };

tags.forEach(x => testTagSuccessful(obj, x));
testTagSuccessful(obj, tag);
});

describe('Parse list', () => {
Expand Down
214 changes: 5 additions & 209 deletions src/features/sicp/parser/__tests__/__snapshots__/ParseJson.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -352,133 +352,11 @@ exports[`Parse object successful 1`] = `
exports[`Parse reference REFERENCE successful 1`] = `
"<div>
<div>
<span>
<p>
Mock Text
</p>
</span>
</div>
</div>"
`;
exports[`Parse section CHAPTER successful 1`] = `
"<div>
<Component>
Title
</Component>
<div>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
</div>
</div>"
`;

exports[`Parse section MATTER successful 1`] = `
"<div>
<Component>
Title
</Component>
<div>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
</div>
</div>"
`;

exports[`Parse section REFERENCES successful 1`] = `
"<div>
<Component>
Title
</Component>
<div>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
</div>
<span>
<p>
Mock Text
</p>
</span>
</div>"
`;
Expand Down Expand Up @@ -522,86 +400,6 @@ exports[`Parse section SECTION successful 1`] = `
</div>"
`;

exports[`Parse section SUBSECTION successful 1`] = `
"<div>
<Component>
Title
</Component>
<div>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
</div>
</div>"
`;

exports[`Parse section SUBSUBSECTION successful 1`] = `
"<div>
<Component>
Title
</Component>
<div>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
<span>
<div className=\\"sicp-text\\">
<span>
<p>
Mock Text
</p>
</span>
<span>
<p>
Mock Text
</p>
</span>
</div>
<br />
</span>
</div>
</div>"
`;

exports[`Parse snippet SNIPPET no eval successful 1`] = `
"<ContextProvider value={{...}}>
<Component>
Expand Down Expand Up @@ -660,8 +458,6 @@ exports[`Parse styling JAVASCRIPTINLINE successful 1`] = `"<code className=\\"b

exports[`Parse styling META successful 1`] = `"<em />"`;

exports[`Parse styling METAPHRASE successful 1`] = `"<p />"`;

exports[`Parse styling TT successful 1`] = `
"<code className=\\"bp3-code\\">
<span>
Expand Down

0 comments on commit 9899ce9

Please sign in to comment.