1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
#
# cli.py
#
# Copyright (C) 2017-2024 Franco Masotti (see /README.md)
#
# This file is part of md-toc.
#
# md-toc 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.
#
# md-toc 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 md-toc. If not, see <http://www.gnu.org/licenses/>.
#
"""Command line interface file."""
import argparse
import sys
import textwrap
from importlib import metadata
from . import generic
from .api import (
build_multiple_tocs,
tocs_equal,
write_strings_on_files_between_markers,
)
from .constants import common_defaults
from .constants import parser as md_parser
PROGRAM_DESCRIPTION = 'Markdown Table Of Contents: Automatically generate a compliant table\nof contents for a markdown file to improve document readability.'
VERSION_NAME = 'md_toc'
VERSION_COPYRIGHT = 'Copyright (C) 2017-2023 Franco Masotti, frnmst'
VERSION_LICENSE = 'License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.'
RETURN_VALUES = 'Return values: 0 ok, 1 error, 2 invalid command, 128 TOC differs from the one in the file (see --diff option)'
try:
VERSION_NUMBER = metadata.distribution('md_toc').version
except metadata.PackageNotFoundError:
VERSION_NUMBER = 'development version (venv)'
PROGRAM_EPILOG = RETURN_VALUES + '\n\n' + VERSION_COPYRIGHT + '\n' + VERSION_LICENSE
class CliToApi():
"""An interface between the CLI and API functions."""
def write_toc(self, args) -> bool:
"""Write the table of contents."""
ordered = False
if args.ordered_list_marker is not None:
list_marker = args.ordered_list_marker
ordered = True
elif args.unordered_list_marker is not None:
list_marker = args.unordered_list_marker
newline_string = args.newline_string
if newline_string == r'\n':
newline_string = '\n'
elif newline_string == r'\r':
newline_string = '\r'
if newline_string == r'\r\n':
newline_string = '\r\n'
toc_struct = build_multiple_tocs(
filenames=args.filename,
ordered=ordered,
no_links=args.no_links,
no_indentation=args.no_indentation,
no_list_coherence=args.no_list_coherence,
keep_header_levels=args.header_levels,
parser=args.parser,
list_marker=list_marker,
skip_lines=args.skip_lines,
constant_ordered_list=args.constant_ordered_list,
newline_string=newline_string,
)
equal: bool = True
if args.in_place:
equal = write_strings_on_files_between_markers(
filenames=args.filename,
strings=toc_struct,
marker=args.toc_marker,
newline_string=newline_string,
)
else:
for i, toc in enumerate(toc_struct):
print(toc, end='')
if args.diff:
equal &= tocs_equal(toc, args.filename[i], args.toc_marker)
diff: bool = False
if equal is False:
diff = True
# Return that the TOC(s) is differing only if the `--diff` argument
# was selected, i.e.: `args.diff`.
return diff & args.diff
class CliInterface():
"""The interface exposed to the final user."""
def __init__(self):
"""Set the parser variable that will be used instead of using create_parser."""
self.parser = self.create_parser()
def _add_filename_argument(self, parser):
# The filename argument is common to all markdown parsers.
# See commit b65cf32.
parser.add_argument(
'filename',
metavar='FILE_NAME',
nargs='*',
help='the I/O file name',
)
def _add_cmark_like_megroup_arguments(self, megroup, parser_name: str):
megroup.add_argument(
'-u',
'--unordered-list-marker',
choices=md_parser[parser_name]['list']['unordered']
['bullet_markers'],
nargs='?',
const=md_parser[parser_name]['list']['unordered']
['default_marker'],
default=md_parser[parser_name]['list']['unordered']
['default_marker'],
help='set the marker and enables unordered list. Defaults to ' +
md_parser[parser_name]['list']['unordered']['default_marker'],
)
megroup.add_argument(
'-o',
'--ordered-list-marker',
choices=md_parser[parser_name]['list']['ordered']
['closing_markers'],
nargs='?',
const=md_parser[parser_name]['list']['ordered']
['default_closing_marker'],
help=('set the marker and enable ordered lists. Defaults to ' +
md_parser[parser_name]['list']['ordered']
['default_closing_marker']),
)
def _add_cmark_like_arguments(self, parser, parser_name: str):
parser.add_argument(
'-c',
'--constant-ordered-list',
action='store_true',
help='intead of progressive numbers use a single integer as \
list marker. This options enables ordered lists',
)
parser.add_argument(
'-l',
'--header-levels',
type=int,
choices=range(1,
md_parser[parser_name]['header']['max_levels'] + 1),
nargs='?',
const=md_parser[parser_name]['header']['default_keep_levels'],
help='set the maximum level of headers to be considered as part \
of the TOC. Defaults to ' +
str(md_parser[parser_name]['header']['default_keep_levels'], ),
)
def create_parser(self):
"""Create the CLI parser."""
parser = argparse.ArgumentParser(
description=PROGRAM_DESCRIPTION,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(PROGRAM_EPILOG),
)
# markdown parser is first positional argument.
# File names are arguments to the subparser.
subparsers = parser.add_subparsers(
dest='parser',
title='markdown parser',
help='<markdown parser> --help',
)
subparsers.required = True
#########################
# github + commonmarker #
#########################
github = subparsers.add_parser(
'github',
aliases=['commonmarker'],
description='Use GitHub Flavored Markdown rules to generate \
an output. If no \
option is selected, the default output will be an \
unordered list with the respective default values \
as listed below',
)
self._add_filename_argument(github)
megroup = github.add_mutually_exclusive_group()
self._add_cmark_like_megroup_arguments(megroup, 'github')
self._add_cmark_like_arguments(github, 'github')
github.set_defaults(header_levels=md_parser['github']['header']
['default_keep_levels'], )
##########
# gitlab #
##########
gitlab = subparsers.add_parser(
'gitlab',
description='Use GitLab Flavored Markdown rules to generate an \
output. If no \
option is selected, the default output will be an \
unordered list with the respective default values \
as listed below',
)
self._add_filename_argument(gitlab)
megroup = gitlab.add_mutually_exclusive_group()
self._add_cmark_like_megroup_arguments(megroup, 'gitlab')
self._add_cmark_like_arguments(gitlab, 'gitlab')
gitlab.set_defaults(header_levels=md_parser['gitlab']['header']
['default_keep_levels'], )
####################
# cmark + Goldmark #
####################
cmark = subparsers.add_parser(
'cmark',
aliases=['goldmark'],
description='Use CommonMark rules to generate an output. If no \
option is selected, the default output will be an \
unordered list with the respective default values \
as listed below',
)
self._add_filename_argument(cmark)
megroup = cmark.add_mutually_exclusive_group()
self._add_cmark_like_megroup_arguments(megroup, 'cmark')
self._add_cmark_like_arguments(cmark, 'cmark')
cmark.set_defaults(header_levels=md_parser['cmark']['header']
['default_keep_levels'], )
#############
# Redcarpet #
#############
redcarpet = subparsers.add_parser(
'redcarpet',
description='Use Redcarpet rules to generate an output. If no \
option is selected, the default output will be an \
unordered list with the respective default values \
as listed below. Gitlab rules are the same as \
Redcarpet except that conflicts are avoided with \
duplicate headers.',
)
self._add_filename_argument(redcarpet)
megroup = redcarpet.add_mutually_exclusive_group()
megroup.add_argument(
'-u',
'--unordered-list-marker',
choices=md_parser['redcarpet']['list']['unordered']
['bullet_markers'],
nargs='?',
const=md_parser['redcarpet']['list']['unordered']
['default_marker'],
default=md_parser['redcarpet']['list']['unordered']
['default_marker'],
help='set the marker and enables unordered list. Defaults to ' +
md_parser['redcarpet']['list']['unordered']['default_marker'],
)
megroup.add_argument(
'-o',
'--ordered-list-marker',
choices=md_parser['redcarpet']['list']['ordered']
['closing_markers'],
nargs='?',
const=md_parser['redcarpet']['list']['ordered']
['default_closing_marker'],
help='set the marker and enables ordered lists. Defaults to ' +
md_parser['redcarpet']['list']['ordered']
['default_closing_marker'],
)
redcarpet.add_argument(
'-c',
'--constant-ordered-list',
action='store_true',
help='intead of progressive numbers use a single integer as list \
marker. This options enables ordered lists',
)
redcarpet.add_argument(
'-l',
'--header-levels',
type=int,
choices=range(
1,
md_parser['redcarpet']['header']['max_levels'] + 1,
),
nargs='?',
const=md_parser['redcarpet']['header']['default_keep_levels'],
help='set the maximum level of headers to be considered as part \
of the TOC. Defaults to ' +
str(md_parser['redcarpet']['header']['default_keep_levels'], ),
)
redcarpet.set_defaults(header_levels=md_parser['redcarpet']['header']
['default_keep_levels'], )
##########
# Common #
##########
no_list_coherence_or_no_indentation = parser.add_mutually_exclusive_group(
)
no_list_coherence_or_no_indentation.add_argument(
'-c',
'--no-list-coherence',
action='store_true',
help='avoids checking for TOC list coherence',
)
no_list_coherence_or_no_indentation.add_argument(
'-i',
'--no-indentation',
action='store_true',
help='avoids adding indentations to the TOC',
)
parser.add_argument(
'-d',
'--diff',
action='store_true',
help=(
'returns 128 if the newly generated TOC differs from the one \
already existing in the file'),
)
parser.add_argument(
'-l',
'--no-links',
action='store_true',
help='avoids adding links to the TOC',
)
parser.add_argument(
'-m',
'--toc-marker',
metavar='TOC_MARKER',
default=common_defaults['toc_marker'],
help=(
'set the string to be used as the marker for positioning the \
table of contents. Put this value between single quotes. \
Defaults to \'' + common_defaults['toc_marker']) + '\'',
)
parser.add_argument(
'-n',
'--newline-string',
choices=[r'\n', r'\r', r'\r\n'],
metavar='NEWLINE_STRING',
type=str,
default=common_defaults['newline_string'],
help=('the string used to separate the lines of the TOC. \
Use single quotes to delimit the string. \
If you output in place all the newlines of the \
input file will be replaced with this value. \
Defaults to ' + repr(common_defaults['newline_string'])),
)
parser.add_argument(
'-p',
'--in-place',
action='store_true',
help='overwrite the input file',
)
parser.add_argument(
'-s',
'--skip-lines',
metavar='SKIP_LINES',
type=int,
default=0,
help='skip parsing of the first selected number of lines. \
Defaults to 0, i.e. do not skip any lines',
)
parser.add_argument(
'-v',
'--version',
action='version',
version=VERSION_NAME + ' ' + VERSION_NUMBER,
)
parser.set_defaults(func=CliToApi().write_toc)
return parser
|