Skip to content

Commit

Permalink
Merge pull request #3 from kn1cht/feature/figure-environment
Browse files Browse the repository at this point in the history
Support figure environment
  • Loading branch information
TANIGUCHI Masaya committed Feb 12, 2019
2 parents da07958 + d83940a commit c672338
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/latex-to-ast/environment/common.ts
Expand Up @@ -44,11 +44,12 @@ export const BeginEnvironment = (

export const EndEnvironment = (context: Context): Parsimmon.Parser<null> =>
Parsimmon((input, i) => {
const m = input.slice(i).match(new RegExp(`^\\\\end\\{${context.name}\\}`));
const p = context.name.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
const m = input.slice(i).match(new RegExp(`^\\\\end\\{${p}\\}`));
if (m !== null) {
return Parsimmon.makeSuccess(i + m[0].length, null);
} else {
return Parsimmon.makeFailure(i, `\\end{${context.name}}`);
return Parsimmon.makeFailure(i, `\\end{${p}}`);
}
});

Expand Down
40 changes: 40 additions & 0 deletions src/latex-to-ast/environment/figure.ts
@@ -0,0 +1,40 @@
/*
* This file is part of textlint-plugin-latex2e
*
* Foobar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Foobar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/

import Parsimmon from "parsimmon";
import { Rules } from "../rules";
import { BeginEnvironment, EndEnvironment, EnvironmentNode } from "./common";

export const Figure = (r: Rules) => {
const context = { name: "" };
const body = Parsimmon.seqMap(
Parsimmon.index,
Parsimmon.index,
(start, end) => ({
start,
end,
name: "figure"
})
);
return Parsimmon.seqObj<EnvironmentNode>(
["name", BeginEnvironment("figure\\\*?", context)],
["arguments", Parsimmon.alt(r.Option, r.Argument).many()],
Parsimmon.whitespace,
["body", body],
EndEnvironment(context)
).node("environment");
};
1 change: 1 addition & 0 deletions src/latex-to-ast/environment/index.ts
Expand Up @@ -16,6 +16,7 @@
*/
export { DisplayMath } from "./displaymath";
export { Environment } from "./common";
export { Figure } from "./figure";
export { InlineMath } from "./inlinemath";
export { Document } from "./document";
export { List } from "./list";
7 changes: 7 additions & 0 deletions src/latex-to-ast/index.ts
Expand Up @@ -167,6 +167,13 @@ export const parse = (text: string) => {
children: node.value.arguments.concat(node.value.body)
});
break;
case "figure":
this.update({
...tmp,
type: ASTNodeTypes.Image,
children: node.value.arguments.concat(node.value.body)
});
break;
default:
this.update({
...tmp,
Expand Down
2 changes: 2 additions & 0 deletions src/latex-to-ast/latex.ts
Expand Up @@ -19,6 +19,7 @@ import Parsimmon from "parsimmon";
import {
Environment,
DisplayMath,
Figure,
InlineMath,
Document,
List
Expand All @@ -35,6 +36,7 @@ export const LaTeX = Parsimmon.createLanguage({
Document,
List,
DisplayMath,
Figure,
InlineMath,
Command,
Verbatim,
Expand Down
13 changes: 13 additions & 0 deletions test/latex-to-ast.test.ts
Expand Up @@ -76,6 +76,19 @@ describe("Parsimmon AST", async () => {
const ast = LaTeX.Program.tryParse(code);
expect(ast.value[0].value.body.value.length).toBe(2);
});
test("figure environment", async () => {
const code = `\\begin{figure}
\\includegraphics[width=5cm]{somefigure.png}
\\caption{This is a caption}
\\end{figure}
\\begin{figure*}
\\includegraphics[width=5cm]{anotherfigure.png}
\\caption{This is an another caption}
\\end{figure*}`;
const ast = LaTeX.Program.tryParse(code);
expect(ast.value[0].value.name).toBe("figure");
expect(ast.value[2].value.name).toBe("figure*");
});
});

describe("TxtNode AST", async () => {
Expand Down

0 comments on commit c672338

Please sign in to comment.