| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import sys |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from .slugify import slugify, DEFAULT_SEPARATOR |
| 8 |
|
| 9 |
|
| 10 |
def parse_args(argv: list[str]) -> argparse.Namespace: |
| 11 |
parser = argparse.ArgumentParser(description="Slug string") |
| 12 |
|
| 13 |
input_group = parser.add_argument_group(description="Input") |
| 14 |
input_group.add_argument("input_string", nargs='*', |
| 15 |
help='Text to slugify') |
| 16 |
input_group.add_argument("--stdin", action='store_true', |
| 17 |
help="Take the text from STDIN") |
| 18 |
|
| 19 |
parser.add_argument("--no-entities", action='store_false', dest='entities', default=True, |
| 20 |
help="Do not convert HTML entities to unicode") |
| 21 |
parser.add_argument("--no-decimal", action='store_false', dest='decimal', default=True, |
| 22 |
help="Do not convert HTML decimal to unicode") |
| 23 |
parser.add_argument("--no-hexadecimal", action='store_false', dest='hexadecimal', default=True, |
| 24 |
help="Do not convert HTML hexadecimal to unicode") |
| 25 |
parser.add_argument("--max-length", type=int, default=0, |
| 26 |
help="Output string length, 0 for no limit") |
| 27 |
parser.add_argument("--word-boundary", action='store_true', default=False, |
| 28 |
help="Truncate to complete word even if length ends up shorter than --max_length") |
| 29 |
parser.add_argument("--save-order", action='store_true', default=False, |
| 30 |
help="When set and --max_length > 0 return whole words in the initial order") |
| 31 |
parser.add_argument("--separator", type=str, default=DEFAULT_SEPARATOR, |
| 32 |
help="Separator between words. By default " + DEFAULT_SEPARATOR) |
| 33 |
parser.add_argument("--stopwords", nargs='+', |
| 34 |
help="Words to discount") |
| 35 |
parser.add_argument("--regex-pattern", |
| 36 |
help="Python regex pattern for disallowed characters") |
| 37 |
parser.add_argument("--no-lowercase", action='store_false', dest='lowercase', default=True, |
| 38 |
help="Activate case sensitivity") |
| 39 |
parser.add_argument("--replacements", nargs='+', |
| 40 |
help="""Additional replacement rules e.g. "|->or", "%%->percent".""") |
| 41 |
parser.add_argument("--allow-unicode", action='store_true', default=False, |
| 42 |
help="Allow unicode characters") |
| 43 |
|
| 44 |
args = parser.parse_args(argv[1:]) |
| 45 |
|
| 46 |
if args.input_string and args.stdin: |
| 47 |
parser.error("Input strings and --stdin cannot work together") |
| 48 |
|
| 49 |
if args.replacements: |
| 50 |
def split_check(repl): |
| 51 |
SEP = '->' |
| 52 |
if SEP not in repl: |
| 53 |
parser.error("Replacements must be of the form: ORIGINAL{SEP}REPLACED".format(SEP=SEP)) |
| 54 |
return repl.split(SEP, 1) |
| 55 |
args.replacements = [split_check(repl) for repl in args.replacements] |
| 56 |
|
| 57 |
if args.input_string: |
| 58 |
args.input_string = " ".join(args.input_string) |
| 59 |
elif args.stdin: |
| 60 |
args.input_string = sys.stdin.read() |
| 61 |
|
| 62 |
if not args.input_string: |
| 63 |
args.input_string = '' |
| 64 |
|
| 65 |
return args |
| 66 |
|
| 67 |
|
| 68 |
def slugify_params(args: argparse.Namespace) -> dict[str, Any]: |
| 69 |
return dict( |
| 70 |
text=args.input_string, |
| 71 |
entities=args.entities, |
| 72 |
decimal=args.decimal, |
| 73 |
hexadecimal=args.hexadecimal, |
| 74 |
max_length=args.max_length, |
| 75 |
word_boundary=args.word_boundary, |
| 76 |
save_order=args.save_order, |
| 77 |
separator=args.separator, |
| 78 |
stopwords=args.stopwords, |
| 79 |
lowercase=args.lowercase, |
| 80 |
replacements=args.replacements, |
| 81 |
allow_unicode=args.allow_unicode |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
def main(argv: list[str] | None = None): # pragma: no cover |
| 86 |
""" Run this program """ |
| 87 |
if argv is None: |
| 88 |
argv = sys.argv |
| 89 |
args = parse_args(argv) |
| 90 |
params = slugify_params(args) |
| 91 |
try: |
| 92 |
print(slugify(**params)) |
| 93 |
except KeyboardInterrupt: |
| 94 |
sys.exit(-1) |
| 95 |
|
| 96 |
|
| 97 |
if __name__ == '__main__': # pragma: no cover |
| 98 |
main() |