Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: Line Length and Wrapping #27

Open
trickeydan opened this issue Jan 21, 2021 · 1 comment
Open

Documentation: Line Length and Wrapping #27

trickeydan opened this issue Jan 21, 2021 · 1 comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Milestone

Comments

@trickeydan
Copy link
Contributor

there's some variation in line wrapping between files – do you have opinions on whether we should hard-wrap docs or not (and if so, at how many columns)? it's already inconsistent in the existing docs, so don't worry about changing anything this pr, but possibly something to consider

I'm not really sure where I sit on this. I'll open an issue and we can discuss this evening.

Originally posted by @trickeydan in #20 (comment)

@trickeydan trickeydan added documentation Improvements or additions to documentation question Further information is requested labels Jan 21, 2021
@trickeydan
Copy link
Contributor Author

I began work on a linter based on rst-lint for this, but ran into issues with the Sphinx parser and custom directives.

Adding my code here for future reference so it doesn't get lost

#!/usr/bin/env python3
"""
Lint the documentation.

Makes use of restructuredtext_lint, but must be a custom script to allow for
custom directives that Sphinx uses.
"""
from enum import Enum
from pathlib import Path
from typing import NamedTuple, Optional, Set

import click
from docutils.parsers.rst.directives import register_directive
from m2r2 import MdInclude
from sphinx.directives.code import Highlight
import restructuredtext_lint

# Load non-standard Sphinx directives
register_directive('highlight', Highlight)
register_directive('mdinclude', MdInclude)


class ProblemLevel(Enum):
    """Level of a lint problem."""

    ERROR = "ERROR"
    WARNING = "WARNING"
    INFO = "INFO"

class LintProblem(NamedTuple):
    """Information about a problem found when linting."""

    file_path: Path
    level: ProblemLevel
    line_number: Optional[int]
    description: str


@click.command("lint-docs")
@click.argument("path", type=click.Path())
def main(path: str) -> None:
    """Lint documentation."""
    problems: Set[LintProblem] = set()
    rst_files = Path(path).rglob("*.rst")
    for file_path in rst_files:
        problems |= rst_lint(file_path)
    
    for p in problems:
        click.secho(f"{p.file_path}:{p.line_number} {p.description}")

def rst_lint(file_path: Path) -> Set[LintProblem]:
    problems: Set[LintProblem] = set()
    errors = restructuredtext_lint.lint_file(file_path)
    for error in errors:
        level_map = {
            1: ProblemLevel.INFO,
            2: ProblemLevel.WARNING,
            3: ProblemLevel.ERROR,
            4: ProblemLevel.ERROR,
        }
        problems.add(
            LintProblem(
                file_path=file_path,
                level=level_map[error.level],
                line_number=error.line,
                description=error.message.replace("\n", " "),
            )
        )
    return problems

if __name__ == "__main__":
    main()

@trickeydan trickeydan added this to the Backlog milestone Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: Idea
Development

No branches or pull requests

1 participant