├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── package.json └── src ├── ReSTProcessor.js ├── index.js ├── mapping.js └── rst-to-ast.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": ["add-module-exports"] 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 jimo1001 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # textlint-plugin-rst 2 | 3 | reStructuredText(`*.rst`) support for [textlint](https://github.com/textlint/textlint "textlint"). 4 | 5 | ## Installation 6 | 7 | To install textlint-plugin-rst, the follow python package must be installed. 8 | 9 | - [docutils-ast-writer](https://github.com/jimo1001/docutils-ast-writer "docutils-ast-writer") 10 | 11 | And run follow command. 12 | 13 | npm install textlint-plugin-rst 14 | 15 | ## Usage 16 | 17 | - Add `--plugin rst` to command options 18 | - or add following codes to `.textlintrc` 19 | ``` 20 | { 21 | "plugins": [ 22 | "rst" 23 | ] 24 | } 25 | ``` 26 | 27 | ## License 28 | MIT 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "textlint-plugin-rst", 3 | "description": "reStructuredText support for textlint", 4 | "version": "0.1.1", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "src", 8 | "lib" 9 | ], 10 | "scripts": { 11 | "build": "babel -s -d lib src", 12 | "watch": "babel -w -s -d lib src", 13 | "prepublish": "npm run --if-present build", 14 | "clean": "rm -frv lib" 15 | }, 16 | "author": "Yoshinobu Fujimoto", 17 | "email": "jimo1001@gmail.com", 18 | "license": "MIT", 19 | "keywords": [ 20 | "textlint", 21 | "plugin", 22 | "rst", 23 | "reStructuredText" 24 | ], 25 | "devDependencies": { 26 | "babel-cli": "^6.6.5", 27 | "babel-plugin-add-module-exports": "^0.1.2", 28 | "babel-preset-es2015": "^6.6.0", 29 | "mocha": "^2.4.5", 30 | "power-assert": "^1.3.1", 31 | "textlint": "^6.0.3" 32 | }, 33 | "dependencies": { 34 | "debug": "^2.2.0", 35 | "structured-source": "^3.0.2", 36 | "traverse": "^0.6.6" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/ReSTProcessor.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | import { parse } from "./rst-to-ast"; 4 | 5 | export default class ReSTProcessor { 6 | constructor(config) { 7 | this.config = config; 8 | } 9 | 10 | static availableExtensions() { 11 | return [ 12 | ".rst", 13 | ".rest" 14 | ]; 15 | } 16 | 17 | processor(ext) { 18 | return { 19 | preProcess(text, filePath) { 20 | return parse(text); 21 | }, 22 | postProcess(messages, filePath) { 23 | return { 24 | messages, 25 | filePath: filePath ? filePath : "" 26 | }; 27 | } 28 | }; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | export default { 4 | Processor: require("./ReSTProcessor") 5 | } 6 | -------------------------------------------------------------------------------- /src/mapping.js: -------------------------------------------------------------------------------- 1 | // LICENSE : MIT 2 | "use strict"; 3 | 4 | export const syntaxMap = { 5 | "document": "Document", 6 | "paragraph": "Paragraph", 7 | "block_quote": "BlockQuote", 8 | "list_item": "ListItem", 9 | "bullet_list": "List", 10 | "title": "Header", 11 | "literal_block": "CodeBlock", 12 | "reference": "Link", 13 | "meta": "Html", 14 | "text": "Str", 15 | "emphasis": "Emphasis", 16 | "strong": "Strong", 17 | "image": "Image", 18 | "inline": "Code" 19 | }; 20 | 21 | export const reSTAttributeKeyMap = { 22 | "tagname": "type", 23 | "rawsource": "raw", 24 | "text": "value" 25 | }; 26 | -------------------------------------------------------------------------------- /src/rst-to-ast.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | import { execSync } from "child_process"; 4 | import traverse from "traverse"; 5 | import StructuredSource from "structured-source"; 6 | import { syntaxMap, reSTAttributeKeyMap } from "./mapping"; 7 | 8 | function filterAndReplaceNodeAttributes(node) { 9 | Object.keys(reSTAttributeKeyMap).forEach(key => { 10 | let v = node[key]; 11 | node[reSTAttributeKeyMap[key]] = v; 12 | if (v !== undefined) { 13 | delete node[key]; 14 | } 15 | }); 16 | } 17 | 18 | /** 19 | * parse reStructuredText and return ast mapped location info. 20 | * @param {string} text 21 | * @returns {TxtNode} 22 | */ 23 | export function parse(text) { 24 | let ast = JSON.parse(execSync("rst2ast -q", {input: text})); 25 | const src = new StructuredSource(text); 26 | traverse(ast).forEach(function (node) { 27 | if (this.notLeaf) { 28 | filterAndReplaceNodeAttributes(node); 29 | // type 30 | if (node.type === null) { 31 | node.type = "text"; 32 | } 33 | node.type = syntaxMap[node.type]; 34 | if (!node.type) { 35 | node.type = "Unknown"; 36 | } 37 | // raw 38 | node.raw = node.raw || node.value || ""; 39 | // loc 40 | let lines = node.raw.split("\n"); 41 | if (node.line) { 42 | node.loc = { 43 | start: {line: node.line.start, column: 0}, 44 | end: {line: node.line.end, column: lines[lines.length-1].length} 45 | }; 46 | node.range = src.locationToRange(node.loc); 47 | delete node.line; 48 | } 49 | } 50 | }); 51 | return ast; 52 | } 53 | --------------------------------------------------------------------------------