sloplint 0.8.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +230 -1
- data/README.md +181 -63
- data/docs/SPEC.md +32 -7
- data/exe/sloplint +2 -1
- data/lib/sloplint/cli.rb +242 -42
- data/lib/sloplint/engine.rb +39 -4
- data/lib/sloplint/output.rb +10 -6
- data/lib/sloplint/rules.rb +215 -4
- data/lib/sloplint/split.rb +375 -0
- data/lib/sloplint/version.rb +1 -1
- metadata +2 -1
data/lib/sloplint/rules.rb
CHANGED
|
@@ -84,6 +84,11 @@ module Sloplint
|
|
|
84
84
|
"and|but|or|nor|yet|too|enough|itself|indeed|which|that|here|there|so" \
|
|
85
85
|
"|time|world|numbers?|roots?|money|estate|user"
|
|
86
86
|
|
|
87
|
+
# The abstract heads the-x-is-the-x repeats across the copula: nouns that
|
|
88
|
+
# cannot name a specific object. the-x-is-not-the-x skips them, so the
|
|
89
|
+
# negated form of these heads is reported once, by the-x-is-the-x.
|
|
90
|
+
THE_X_HEADS = "reason|problem|question|lesson|difference|trick|move|goal|tell|pattern|insight|takeaway|shift|bet|catch|bottleneck|issue|game|cause|failure|magic|challenge|tension|irony|paradox|trap"
|
|
91
|
+
|
|
87
92
|
RULES = [
|
|
88
93
|
Rule.new(
|
|
89
94
|
id: "no-x-no-y",
|
|
@@ -759,7 +764,7 @@ module Sloplint
|
|
|
759
764
|
# negation ("isn't the reason") both count. The sentence-initial
|
|
760
765
|
# capital is not required, so "and the reason … is the reason …"
|
|
761
766
|
# flags too.
|
|
762
|
-
pattern: /\bthe\s+(
|
|
767
|
+
pattern: /\bthe\s+(#{THE_X_HEADS})\b
|
|
763
768
|
(?:[^.!?;:,\n]|\r?\n(?!\s*\n)){1,50}?\bis(?:n['’]t|\s+not)?\s+the\s+\1
|
|
764
769
|
(?=[,.;:!?]|\s+(?:of|for|with|in|on|to|at|behind|about|that|which|why|here|there|the|a|an|this|these|those|my|our|your|their|its|his|her|it|they|we|you|i|he|she|every|each|most|many|some|any|no|nobody|everyone|people|[a-z]+s)\b)/ix,
|
|
765
770
|
message: '"The X … is the X …" equates by repeating the head noun.',
|
|
@@ -797,6 +802,63 @@ module Sloplint
|
|
|
797
802
|
"two things while naming neither; it sounds like a diagnosis and " \
|
|
798
803
|
"delivers a tautology. Careful writers say what the second thing is."
|
|
799
804
|
),
|
|
805
|
+
Rule.new(
|
|
806
|
+
id: "the-x-is-not-the-x",
|
|
807
|
+
category: "cadence",
|
|
808
|
+
severity: "warning",
|
|
809
|
+
confidence: "medium",
|
|
810
|
+
# The negated twin of the-x-is-the-x with any head noun: "the job you
|
|
811
|
+
# applied for is not the job you will do", "the one you want is not the
|
|
812
|
+
# one that runs the studio". The same noun sits on both sides of a
|
|
813
|
+
# negated copula, caught by a backreference, and a clause on either side
|
|
814
|
+
# splits it into two things the sentence never tells apart. The
|
|
815
|
+
# repetition is the tell: a writer who knew what separates the two would
|
|
816
|
+
# name it instead of mirroring the noun.
|
|
817
|
+
#
|
|
818
|
+
# The narrowings are structural. The clause between the two heads is
|
|
819
|
+
# capped at fifty characters and may not hold sentence punctuation, a
|
|
820
|
+
# comma, semicolon or colon, so the heads share one clause. The second
|
|
821
|
+
# head may not be followed by "of", which names the difference ("the
|
|
822
|
+
# voice was not the voice of her brother"), and "not the same X" never
|
|
823
|
+
# matches because "same" sits between "the" and the head. "same" is not
|
|
824
|
+
# a head either. The-x-is-the-x's abstract heads are skipped, since that
|
|
825
|
+
# rule already reports their negated form.
|
|
826
|
+
pattern: /\bthe#{WRAP_GAP}+(?!(?:#{THE_X_HEADS}|same)\b)([[:alpha:]]{2,})\b
|
|
827
|
+
(?:[^.!?;:,\n]|\r?\n(?!\s*\n)){0,50}?
|
|
828
|
+
\b(?:is|are|was|were)(?:n['’]t|#{WRAP_GAP}+not)#{WRAP_GAP}+the#{WRAP_GAP}+\1\b
|
|
829
|
+
(?!['’-]|#{WRAP_GAP}+of\b)/ix,
|
|
830
|
+
message: '"The X … is not the X …" splits one noun in two and names neither half.',
|
|
831
|
+
suggestion: "Name the thing you mean; say what separates it from the other.",
|
|
832
|
+
examples_bad: [
|
|
833
|
+
"The job you applied for is not the job you will do.",
|
|
834
|
+
"Agencies staff two kinds of lead, and the one you need is not the one who pitches.",
|
|
835
|
+
"The customer who signs the contract isn't the customer who uses the product.",
|
|
836
|
+
"The meeting on the calendar was not the meeting that mattered.",
|
|
837
|
+
"The ones who ship first are not the ones who ship well.",
|
|
838
|
+
# A hard-wrapped clause survives one newline.
|
|
839
|
+
"The hire you need\nis not the hire the board will pitch."
|
|
840
|
+
],
|
|
841
|
+
examples_ok: [
|
|
842
|
+
# "of" after the second head names the difference.
|
|
843
|
+
"The voice behind the door was not the voice of her brother.",
|
|
844
|
+
# "the same X" is an identity statement.
|
|
845
|
+
"The captain on the bridge tonight isn't the same captain who hired me.",
|
|
846
|
+
"The same caution is not the same thing as fear.",
|
|
847
|
+
# Different heads.
|
|
848
|
+
"The bug in staging is not the cause of the outage.",
|
|
849
|
+
# A sentence boundary splits the clause.
|
|
850
|
+
"The plan was ready. It is not the plan we drew up.",
|
|
851
|
+
# A comma is a boundary: the heads must share a clause.
|
|
852
|
+
"The build passed, and it is not the build we ship.",
|
|
853
|
+
# A paragraph break ends the frame.
|
|
854
|
+
"The draft was long\n\nand it is not the draft we sent."
|
|
855
|
+
],
|
|
856
|
+
rationale: "Repeating one noun on both sides of 'is not' splits it into two things " \
|
|
857
|
+
"without saying what tells them apart. It sounds like a distinction and " \
|
|
858
|
+
"withholds it, so the reader has to read on to learn which one is meant. " \
|
|
859
|
+
"People write the shape when the noun really is one thing seen twice, " \
|
|
860
|
+
"which is why the confidence is medium."
|
|
861
|
+
),
|
|
800
862
|
Rule.new(
|
|
801
863
|
id: "same-determiner-chain",
|
|
802
864
|
category: "cadence",
|
|
@@ -3035,18 +3097,27 @@ module Sloplint
|
|
|
3035
3097
|
category: "cadence",
|
|
3036
3098
|
severity: "info",
|
|
3037
3099
|
confidence: "low",
|
|
3038
|
-
|
|
3100
|
+
# A comma before the first word, across any whitespace, means the tail
|
|
3101
|
+
# of a longer series; \K drops that prefix so the note points at the
|
|
3102
|
+
# first word, and the word-character guard keeps it from starting mid-word.
|
|
3103
|
+
pattern: /(?:\A|(?<=[^,\s]))\s*\K(?<![\w'-])[\w'-]+,\s+[\w'-]+,\s+(?:and\s+)?[\w'-]+[.!?]/,
|
|
3039
3104
|
message: "Three single words in a comma series closing a sentence (heuristic; high false-positive).",
|
|
3040
3105
|
suggestion: "Fine in moderation; watch for the AI habit of ending on triplets.",
|
|
3041
3106
|
examples_bad: ["It was fast, cheap, and simple."],
|
|
3042
3107
|
examples_ok: [
|
|
3043
3108
|
"We met on Tuesday afternoon.",
|
|
3044
3109
|
# Phrasal items do not match; the closing two slots take one word each.
|
|
3045
|
-
"It was very fast, very cheap, and very simple."
|
|
3110
|
+
"It was very fast, very cheap, and very simple.",
|
|
3111
|
+
# Four or more items are a list, not a triad.
|
|
3112
|
+
"Pack tents, stoves, rope, lamps, and maps.",
|
|
3113
|
+
"We grow beans, leeks, chard, kale.",
|
|
3114
|
+
"Pack tents, fly-sheets, pegs, and rope.",
|
|
3115
|
+
"Load the van with crates,\n ladders, buckets, and brooms."
|
|
3046
3116
|
],
|
|
3047
3117
|
rationale: "Rule-of-three endings are a model habit, but humans use them too — off by default. " \
|
|
3048
3118
|
"The closing two items must be single words; a triad of phrases is three ordinary " \
|
|
3049
|
-
"list items to a regex, so those are left unflagged rather than guessed at."
|
|
3119
|
+
"list items to a regex, so those are left unflagged rather than guessed at. The last " \
|
|
3120
|
+
"three words of a longer series are not a triad and are left alone too."
|
|
3050
3121
|
),
|
|
3051
3122
|
# clause-triad-then was cut. The pattern (comma-clause, comma-clause,
|
|
3052
3123
|
# "then" clause) had no way to require the clauses actually be parallel
|
|
@@ -3262,6 +3333,146 @@ module Sloplint
|
|
|
3262
3333
|
"why this is a question rather than a verdict; a draft full of them, " \
|
|
3263
3334
|
"especially as list items, should be read as a warning."
|
|
3264
3335
|
),
|
|
3336
|
+
Rule.new(
|
|
3337
|
+
id: "negative-subject",
|
|
3338
|
+
category: "cadence",
|
|
3339
|
+
severity: "warning",
|
|
3340
|
+
confidence: "medium",
|
|
3341
|
+
# A negative pronoun (Nothing, Nobody, No one, None) as the subject of
|
|
3342
|
+
# a clause, where the clause carries one of two structural markers
|
|
3343
|
+
# instead of stopping at the bare subject and verb. Bare "Nothing
|
|
3344
|
+
# happened." and "No one knows." are the common case in ordinary
|
|
3345
|
+
# prose and are not matched on their own; a regex cannot tell a
|
|
3346
|
+
# lexical verb from any other word, so the narrowing has to come from
|
|
3347
|
+
# what surrounds the subject, not from the verb.
|
|
3348
|
+
#
|
|
3349
|
+
# Two other frames were probed and cut, both for the same reason: a
|
|
3350
|
+
# regex can't tell a report of a real, specific absence from the
|
|
3351
|
+
# vague version the tell is aimed at.
|
|
3352
|
+
# - "No" plus a common noun carrying the role ("No vendor holds a
|
|
3353
|
+
# contract.") looked like the same kind of narrowing, but it is
|
|
3354
|
+
# indistinguishable from the ordinary way English negates a
|
|
3355
|
+
# subject -- "No installation is necessary.", "No magic is
|
|
3356
|
+
# involved.", "No exceptions are raised on HTTP timeouts." are
|
|
3357
|
+
# three hits from a 266k-word read of installed gems' READMEs,
|
|
3358
|
+
# all fair, none of them slop. On RAID it fired at or above the
|
|
3359
|
+
# model rate in every domain that had any hits at all.
|
|
3360
|
+
# - A bare pronoun with a prepositional phrase before the verb
|
|
3361
|
+
# ("Nobody on the on-call page owns the service.") looked
|
|
3362
|
+
# narrower, since the PP seemed to be dressing up a vague
|
|
3363
|
+
# subject. It isn't: "Nobody on the team has reviewed the
|
|
3364
|
+
# migration yet.", "Nothing in the runbook mentions this failure
|
|
3365
|
+
# mode." and "No one from the platform team joined the
|
|
3366
|
+
# postmortem." have the same shape and are exactly the opposite
|
|
3367
|
+
# of the tell -- a real, specific absence, with the PP naming the
|
|
3368
|
+
# scope. Nothing here can tell those apart from "Nobody on the
|
|
3369
|
+
# on-call page owns the service.", which has the identical shape.
|
|
3370
|
+
# Neither frame has a structural narrowing left to try that does not
|
|
3371
|
+
# become a word or exception list, so both ship cut rather than
|
|
3372
|
+
# forced; the maintainer's cases may need a judge rule instead.
|
|
3373
|
+
#
|
|
3374
|
+
# A clause starts at the beginning of the text, at a full stop,
|
|
3375
|
+
# question mark, exclamation mark, semicolon or colon, or at a line
|
|
3376
|
+
# break -- actually-not-x's clause-start set. Frame 1 below adds one
|
|
3377
|
+
# more: after a coordinating conjunction (and, but, or, so, yet, nor),
|
|
3378
|
+
# since that is how a second clause opens inside one sentence
|
|
3379
|
+
# ("...and nothing is queued"). Frame 2 stays on the plain
|
|
3380
|
+
# clause-start set: a conjunction most often joins two noun phrases
|
|
3381
|
+
# inside one clause, not two clauses ("two real roots and no real
|
|
3382
|
+
# numbers"), and nothing here can tell the two apart, so only the
|
|
3383
|
+
# frame that already requires its own internal "and" reaches past it.
|
|
3384
|
+
#
|
|
3385
|
+
# The two frames, each pinned by its own examples_ok:
|
|
3386
|
+
# 1. Two negative-subject clauses joined by "and" -- the pronoun
|
|
3387
|
+
# pairing repeats, which is the cadence itself ("Nothing is
|
|
3388
|
+
# deployed and nothing is queued.").
|
|
3389
|
+
# 2. A negative-subject clause whose object carries its own negated
|
|
3390
|
+
# relative clause ("nothing gets merged that does not get
|
|
3391
|
+
# reviewed."). The relative clause's negation is the full closed
|
|
3392
|
+
# set of auxiliaries and their contractions -- does/do/did,
|
|
3393
|
+
# has/have/had, is/are/was/were -- not just does/do/did. What
|
|
3394
|
+
# follows the negation runs one to ten words, atomic, not one to
|
|
3395
|
+
# three: "that does not pass the full suite first" needs the
|
|
3396
|
+
# room, and the boundary (a full stop or a clause break) is what
|
|
3397
|
+
# keeps the run from drifting into the next clause, not the cap.
|
|
3398
|
+
#
|
|
3399
|
+
# A clause the rule flags does not have to be the last clause in the
|
|
3400
|
+
# sentence: what ends it is a full stop, or a clause break (a comma
|
|
3401
|
+
# before a coordinating conjunction, or a bare comma, semicolon or
|
|
3402
|
+
# colon), so "Nothing merges here, and nothing that fails review
|
|
3403
|
+
# merges elsewhere either." is caught by the first clause rather than
|
|
3404
|
+
# missed for want of a period at the end of the sentence. Each
|
|
3405
|
+
# frame's word count between the subject and that boundary is capped
|
|
3406
|
+
# and atomic, so an unpunctuated run cannot backtrack
|
|
3407
|
+
# catastrophically. Frame 1's first predicate excludes "and" as one
|
|
3408
|
+
# of its words, and frame 2's first predicate excludes "that": each
|
|
3409
|
+
# is followed by that literal word, and an atomic group cannot
|
|
3410
|
+
# backtrack to give it back once it has swallowed it as an ordinary
|
|
3411
|
+
# word instead.
|
|
3412
|
+
#
|
|
3413
|
+
# A clause start may also carry one or two markdown emphasis markers
|
|
3414
|
+
# (*, _) directly before the subject, since a markdown document bolds
|
|
3415
|
+
# or italicizes the opening of a sentence or a list item structurally
|
|
3416
|
+
# ("*Nothing is posted and nothing is vacant..."), the same way a
|
|
3417
|
+
# sentence start already tolerates a list marker elsewhere in this
|
|
3418
|
+
# file. The marker is inside the match, same as the subject.
|
|
3419
|
+
pattern: /(?:\A|(?<=[.;:!?\n])[ \t]*|\b(?:and|but|or|so|yet|nor)\b[ \t]+)\K(?:[*_]{1,2})?
|
|
3420
|
+
(?:Nothing|Nobody|No[ \t]+one|None)(?>(?:[ \t]+(?!and\b)[\w'’-]+){1,8})[ \t]+and[ \t]+
|
|
3421
|
+
(?:Nothing|Nobody|No[ \t]+one|None)(?>(?:[ \t]+[\w'’-]+){1,8})
|
|
3422
|
+
(?:[.!?]|,[ \t]+(?:and|but|or|so|yet|nor)\b|[,;:])
|
|
3423
|
+
|
|
|
3424
|
+
(?:\A|(?<=[.;:!?\n])[ \t]*)\K(?:[*_]{1,2})?
|
|
3425
|
+
(?:Nothing|Nobody|No[ \t]+one|None)(?>(?:[ \t]+(?!that\b)[\w'’-]+){1,3})[ \t]+that[ \t]+
|
|
3426
|
+
(?:(?:does|do|did|has|have|had|is|are|was|were)[ \t]+not|(?:doesn|don|didn|hasn|haven|hadn|isn|aren|wasn|weren)['’]t)
|
|
3427
|
+
(?>(?:[ \t]+[\w'’-]+){1,10})
|
|
3428
|
+
(?:[.!?]|,[ \t]+(?:and|but|or|so|yet|nor)\b|[,;:])
|
|
3429
|
+
/ix,
|
|
3430
|
+
message: '"Nothing/Nobody/No one" as the subject names no one and nothing specific.',
|
|
3431
|
+
suggestion: "Name the specific thing that is missing, or who is responsible for it.",
|
|
3432
|
+
examples_bad: [
|
|
3433
|
+
"Nothing is deployed and nothing is queued.",
|
|
3434
|
+
"The queue is empty; nothing gets merged that does not get reviewed.",
|
|
3435
|
+
# A clause break, not the end of the sentence, closes each frame.
|
|
3436
|
+
"Nobody merges anything that hasn't passed review.",
|
|
3437
|
+
"Nothing is staged and nothing is promoted this week, and the freeze holds through Friday.",
|
|
3438
|
+
# A longer relative-clause tail than three words.
|
|
3439
|
+
"One rule holds every release: nothing ships that does not pass the full suite first.",
|
|
3440
|
+
# A markdown emphasis marker opening the sentence.
|
|
3441
|
+
"*Nothing is queued and nothing is running on the batch cluster tonight."
|
|
3442
|
+
],
|
|
3443
|
+
examples_ok: [
|
|
3444
|
+
# The bare pronoun and a plain verb, with neither of the two frames.
|
|
3445
|
+
"No one knows.",
|
|
3446
|
+
"Nothing happened.",
|
|
3447
|
+
"Nobody owns the service.",
|
|
3448
|
+
# A single clause, not a pairing: no second "and nothing ...".
|
|
3449
|
+
"Nothing is deployed.",
|
|
3450
|
+
# A conjunction clause start with a plain verb, no pairing.
|
|
3451
|
+
"But nobody replied.",
|
|
3452
|
+
# "and nothing" inside one clause, not a second full clause.
|
|
3453
|
+
"The team reviewed the proposal and nothing else mattered.",
|
|
3454
|
+
# "and" joining two noun phrases inside one clause ("has two real
|
|
3455
|
+
# roots and no real numbers"), not opening a second clause -- the
|
|
3456
|
+
# conjunction clause-start is frame 1's alone. Also pins the cut
|
|
3457
|
+
# "No" + noun frame: this stays quiet without it.
|
|
3458
|
+
"The equation has two real roots and no real numbers outside that range.",
|
|
3459
|
+
# The cut "No" + noun frame's own shape, left quiet on purpose: a
|
|
3460
|
+
# determiner and a common noun, not one of the pronouns.
|
|
3461
|
+
"No vendor holds a contract.",
|
|
3462
|
+
# The cut PP frame's shape: a real, specific absence, the
|
|
3463
|
+
# prepositional phrase naming the scope, not a vague subject
|
|
3464
|
+
# dressed up to look concrete. The maintainer's own examples.
|
|
3465
|
+
"Nobody on the team has reviewed the migration yet.",
|
|
3466
|
+
"Nothing in the runbook mentions this failure mode.",
|
|
3467
|
+
"No one from the platform team joined the postmortem."
|
|
3468
|
+
],
|
|
3469
|
+
rationale: "A negative pronoun standing as the subject names nothing specific, and a " \
|
|
3470
|
+
"sentence built to look concrete around it -- a paired clause, a negated " \
|
|
3471
|
+
"relative clause -- reads as if it said something when the subject still " \
|
|
3472
|
+
"names no one and nothing. Bare 'Nothing happened' is ordinary prose and is " \
|
|
3473
|
+
"left alone; the tell is the extra structure dressing up a subject that was " \
|
|
3474
|
+
"never named."
|
|
3475
|
+
),
|
|
3265
3476
|
Rule.new(
|
|
3266
3477
|
id: "quip-question",
|
|
3267
3478
|
category: "reader-address",
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rules"
|
|
4
|
+
require_relative "engine"
|
|
5
|
+
|
|
6
|
+
module Sloplint
|
|
7
|
+
# Splits a document into paragraphs and sentences, keeping each one's offset
|
|
8
|
+
# and length in the original text so a note can land on the file as written.
|
|
9
|
+
#
|
|
10
|
+
# The regex engine does not use this. It exists for the judge and for any
|
|
11
|
+
# other tool that asks questions about a paragraph or a sentence rather than
|
|
12
|
+
# matching a pattern across the whole text. See docs/JUDGE.md "Splitting".
|
|
13
|
+
module Split
|
|
14
|
+
# text is the span as the file has it, which is what a note quotes.
|
|
15
|
+
# asked is the same span with what --markdown skips taken out, which is
|
|
16
|
+
# what the model is shown. They are the same string without --markdown.
|
|
17
|
+
Sentence = Data.define(:offset, :length, :text, :asked)
|
|
18
|
+
Paragraph = Data.define(:offset, :length, :text, :asked, :sentences)
|
|
19
|
+
|
|
20
|
+
# Furniture: a line that is not prose, whatever a regex would think.
|
|
21
|
+
# Headings, list items, table rows, block quotes, horizontal rules,
|
|
22
|
+
# reference-style link definitions and a lone image or link line.
|
|
23
|
+
FURNITURE = /\A[ \t]*(?:\#{1,6}[ \t]|[-*+][ \t]|\||>|[-*_]{3,}[ \t]*\z|\[[^\]]+\]:[ \t]|!\[)/
|
|
24
|
+
|
|
25
|
+
# An ordered-list item, which is furniture too but only sometimes: see
|
|
26
|
+
# ordered_item?.
|
|
27
|
+
ORDERED = /\A[ \t]*(\d+)[.)][ \t]/
|
|
28
|
+
|
|
29
|
+
# Abbreviations whose trailing period does not end a sentence. Case
|
|
30
|
+
# matters: "No." is an abbreviation, "no." is the end of a sentence.
|
|
31
|
+
ABBREV = /\b(?:e\.g|i\.e|vs|etc|cf|Fig|No|Dr|Mr|Mrs|Ms|St|Inc|Ltd|Sec|Ch|Vol)\.\z/
|
|
32
|
+
|
|
33
|
+
# The other period that does not end a sentence, matched on its shape
|
|
34
|
+
# rather than on a list of words: letters written one at a time with a
|
|
35
|
+
# period after each, which is "U.S.", "U.K.", "e.g.", "i.e." and the rest
|
|
36
|
+
# of them. Two pairs at least, so the last letter of a word is not one:
|
|
37
|
+
# "bigger than AT&T." and "a free upgrade from 512K." still end their
|
|
38
|
+
# sentences. The run is bounded, so a piece is read in one pass.
|
|
39
|
+
#
|
|
40
|
+
# One period of this shape is left alone: a single capital standing as a
|
|
41
|
+
# word. It is an initial in "J. K. Rowling wrote the book.", which is
|
|
42
|
+
# split into three sentences here, and it is a whole sentence in "Service
|
|
43
|
+
# A. Service B handles the rest.", which a design document writes far more
|
|
44
|
+
# often than it names anyone by initial.
|
|
45
|
+
#
|
|
46
|
+
# What the run costs instead: a sentence that really ends in one of these
|
|
47
|
+
# joins the one after it, so "It was in the U.S. It cost $2." is read as
|
|
48
|
+
# one sentence.
|
|
49
|
+
INITIALS = /(?<![[:alpha:]])(?:[[:alpha:]]\.){2,6}\z/
|
|
50
|
+
|
|
51
|
+
# A number and a period with nothing else between two boundaries is an
|
|
52
|
+
# enumerator, not a sentence: the "2." of a list item that CommonMark
|
|
53
|
+
# keeps inside the paragraph above it, because a list may interrupt a
|
|
54
|
+
# paragraph only when it starts at 1. The whole piece has to be the
|
|
55
|
+
# number, so a year still closes the sentence it sits in -- "It was
|
|
56
|
+
# rewritten in\n2021. Then it shipped." is two sentences.
|
|
57
|
+
ENUMERATOR = /\A\d+\.\z/
|
|
58
|
+
|
|
59
|
+
# The judge's view of Markdown differs from the regex engine's in one
|
|
60
|
+
# way. Fenced code and comments become spaces, as there, but an inline
|
|
61
|
+
# code span or a URL becomes a run of this character, same length: it is
|
|
62
|
+
# a word in its sentence, so "`x` runs fast." starts at the backtick and
|
|
63
|
+
# "Done. `x` runs." is two sentences. Blanked to spaces, the first lost
|
|
64
|
+
# its head and the second merged. It is a private-use codepoint, which
|
|
65
|
+
# ordinary prose cannot contain, so a line of real text is never taken
|
|
66
|
+
# for a blanked one -- a line reading "XXX" used to be.
|
|
67
|
+
INLINE = "\uE000"
|
|
68
|
+
|
|
69
|
+
# A sentence ends at .!? (with an optional closing quote or bracket)
|
|
70
|
+
# followed by whitespace and a capital, digit, opening quote/bracket, or
|
|
71
|
+
# a code span, which starts a sentence as any other word does.
|
|
72
|
+
BOUNDARY = /(?<=[.!?]|[.!?]["'”’)\]])\s+(?=["'“‘(\[A-Z0-9#{INLINE}])/
|
|
73
|
+
|
|
74
|
+
# The split keeps its separators, so one of the blocks it hands back is
|
|
75
|
+
# the break itself. Both of these are written out here rather than inside
|
|
76
|
+
# the walk, where the interpolation would build the same pattern again for
|
|
77
|
+
# every block of every document.
|
|
78
|
+
BREAK_ONLY = /\A#{PARA_BREAK}\z/
|
|
79
|
+
KEEP_BREAK = /(#{PARA_BREAK})/
|
|
80
|
+
KEEP_BOUNDARY = /(#{BOUNDARY})/
|
|
81
|
+
|
|
82
|
+
module_function
|
|
83
|
+
|
|
84
|
+
# text: the source. Furniture lines -- headings (# and the setext title-
|
|
85
|
+
# plus-underline form), bullets, ordered items, table rows, block
|
|
86
|
+
# quotes, horizontal rules and reference lines -- are blanked whether or
|
|
87
|
+
# not markdown is set, because their shape alone says they are not
|
|
88
|
+
# prose. markdown additionally blanks fenced code, HTML
|
|
89
|
+
# comments and URLs before splitting, and the furniture that only shows
|
|
90
|
+
# once a document is read as Markdown: front matter, an indented code
|
|
91
|
+
# block, a lone HTML tag line, and a line that is only a code span or a
|
|
92
|
+
# URL. Splitting happens on the blanked copy; the texts returned are cut
|
|
93
|
+
# from the original at the same offsets, so an excerpt is always a
|
|
94
|
+
# string that is in the file. Blanking is character for character, so
|
|
95
|
+
# the offsets agree.
|
|
96
|
+
def paragraphs(text, markdown: false)
|
|
97
|
+
scan, asked = blank_furniture(markdown ? blank(text) : text, markdown ? blank_blocks(text) : text, markdown:)
|
|
98
|
+
# A document with nothing to leave out is asked about as written, and
|
|
99
|
+
# then every text is cut once instead of twice.
|
|
100
|
+
asked = text if asked == text
|
|
101
|
+
written = Cursor.new(text)
|
|
102
|
+
shown = asked.equal?(text) ? nil : Cursor.new(asked)
|
|
103
|
+
out = []
|
|
104
|
+
at = 0
|
|
105
|
+
# Split keeping the separators, and walk the offsets arithmetically:
|
|
106
|
+
# String#index with a start position rescans from 0 on non-ASCII text.
|
|
107
|
+
scan.split(KEEP_BREAK).each do |block|
|
|
108
|
+
start = at
|
|
109
|
+
at += block.length
|
|
110
|
+
next if block.match?(BREAK_ONLY)
|
|
111
|
+
|
|
112
|
+
# Both ends trimmed by the same measure. String#strip takes a leading
|
|
113
|
+
# NUL off as well as whitespace and /\A\s*/ does not, so a block that
|
|
114
|
+
# starts with one used to put every offset in the paragraph one
|
|
115
|
+
# character early and cut its last character off.
|
|
116
|
+
from_lead = block.lstrip
|
|
117
|
+
lead = block.length - from_lead.length
|
|
118
|
+
body = from_lead.rstrip
|
|
119
|
+
next if body.empty?
|
|
120
|
+
|
|
121
|
+
offset = start + lead
|
|
122
|
+
# The paragraph is cut out of the original once, and its sentences are
|
|
123
|
+
# cut out of that: cutting each of them out of the whole document by
|
|
124
|
+
# character offset is what made the walk take the square of the
|
|
125
|
+
# document's size.
|
|
126
|
+
para = written.cut(offset, body.length)
|
|
127
|
+
para_asked = shown ? shown.cut(offset, body.length) : para
|
|
128
|
+
sents = sentences(body, base: offset, text: para, asked: para_asked)
|
|
129
|
+
# A one-sentence block ending in a colon is the lead-in to whatever
|
|
130
|
+
# follows (a code block, a list), not a paragraph.
|
|
131
|
+
next if markdown && sents.size == 1 && body.end_with?(":")
|
|
132
|
+
|
|
133
|
+
unless sents.empty?
|
|
134
|
+
flat = squash(para)
|
|
135
|
+
out << Paragraph.new(offset:, length: body.length, text: flat, sentences: sents,
|
|
136
|
+
asked: para.equal?(para_asked) ? flat : squash(para_asked))
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
out
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Sentences of one paragraph. base is where the paragraph starts in the
|
|
143
|
+
# document, which each sentence's offset is counted from. text is the
|
|
144
|
+
# paragraph as the file has it and asked is the copy the model is shown,
|
|
145
|
+
# both cut at the paragraph's own offsets; a sentence is cut out of them
|
|
146
|
+
# and its whitespace collapsed, so hard-wrapped lines read as one. A
|
|
147
|
+
# caller that hands over a bare paragraph and neither copy gets the body
|
|
148
|
+
# itself as both.
|
|
149
|
+
def sentences(body, base: 0, text: nil, asked: nil)
|
|
150
|
+
text ||= body
|
|
151
|
+
asked ||= text
|
|
152
|
+
# The same cursors as a paragraph is cut with, for the same reason: a
|
|
153
|
+
# document whose sentences all sit in one paragraph is one long string
|
|
154
|
+
# to cut them out of.
|
|
155
|
+
written = Cursor.new(text)
|
|
156
|
+
shown = asked.equal?(text) ? nil : Cursor.new(asked)
|
|
157
|
+
cut = lambda do |at, length|
|
|
158
|
+
flat = squash(written.cut(at, length))
|
|
159
|
+
Sentence.new(offset: base + at, length:, text: flat,
|
|
160
|
+
asked: shown ? squash(shown.cut(at, length)) : flat)
|
|
161
|
+
end
|
|
162
|
+
parts = []
|
|
163
|
+
buf_start = nil
|
|
164
|
+
pos = 0
|
|
165
|
+
body.split(KEEP_BOUNDARY).each do |piece|
|
|
166
|
+
at = pos
|
|
167
|
+
pos = at + piece.length
|
|
168
|
+
next if piece.match?(/\A\s+\z/)
|
|
169
|
+
|
|
170
|
+
buf_start ||= at
|
|
171
|
+
# The abbreviation ends where the piece does, and a piece ends at a
|
|
172
|
+
# .!? that a boundary follows, so it is always inside this piece: the
|
|
173
|
+
# whole buffer never has to be cut out of the body to see it.
|
|
174
|
+
next if piece.match?(ABBREV) || piece.match?(INITIALS) || piece.match?(ENUMERATOR)
|
|
175
|
+
|
|
176
|
+
parts << cut.call(buf_start, pos - buf_start)
|
|
177
|
+
buf_start = nil
|
|
178
|
+
end
|
|
179
|
+
parts << cut.call(buf_start, pos - buf_start) if buf_start
|
|
180
|
+
parts
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def squash(s) = s.gsub(/\s+/, " ").strip
|
|
184
|
+
|
|
185
|
+
def blank(text)
|
|
186
|
+
text.gsub(Engine::MARKDOWN_NOISE) do |s|
|
|
187
|
+
Regexp.last_match[:inline] ? INLINE * s.length : s.gsub(/[^\n]/, " ")
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The copy the model is shown: fenced code and HTML comments go, because
|
|
192
|
+
# --markdown says they do, and a code span or URL stays as written,
|
|
193
|
+
# because it is a word of its sentence and the model has to read it.
|
|
194
|
+
def blank_blocks(text)
|
|
195
|
+
text.gsub(Engine::MARKDOWN_NOISE) do |s|
|
|
196
|
+
Regexp.last_match[:inline] ? s : s.gsub(/[^\n]/, " ")
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Blank furniture lines to same-length spaces, and the indented lines that
|
|
201
|
+
# continue a blanked one: a wrapped bullet is one bullet, and its second
|
|
202
|
+
# line is not a sentence of its own. A line that is only code spans or
|
|
203
|
+
# URLs (a command on a line of its own, a bare link) is furniture too.
|
|
204
|
+
LONE_INLINE = /\A[ \t]*#{INLINE}[ \t#{INLINE}]*\z/
|
|
205
|
+
# The same line with sentence punctuation after it. A URL stops before
|
|
206
|
+
# that punctuation now, so the full stop on a bare link line is written
|
|
207
|
+
# there rather than part of the link.
|
|
208
|
+
LONE_INLINE_ENDED = /\A[ \t]*#{INLINE}[ \t#{INLINE}]*[.,;:!?)\]]+\z/
|
|
209
|
+
|
|
210
|
+
# A bullet. FURNITURE matches one too, among everything else it matches;
|
|
211
|
+
# this is here because a bullet and an ordered item are the only two
|
|
212
|
+
# kinds of furniture a continuation line can belong to.
|
|
213
|
+
BULLET = /\A[ \t]*[-*+][ \t]/
|
|
214
|
+
# The second line of a list item, indented under the first.
|
|
215
|
+
CONTINUED = /\A(?:[ ]{2,}|\t)\S/
|
|
216
|
+
# An indented code block: four spaces or a tab. Two spaces are still
|
|
217
|
+
# prose, so an indented paragraph under a heading is kept, which is what
|
|
218
|
+
# CommonMark says as well -- a code block starts at four.
|
|
219
|
+
CODE_INDENT = /\A(?:[ ]{4}|\t)/
|
|
220
|
+
# A line that is one HTML tag, opening or closing, matched on that shape
|
|
221
|
+
# rather than on a list of element names: <details>, <div>, <br/> and
|
|
222
|
+
# whatever else a document drops into Markdown all look the same from
|
|
223
|
+
# here. A sentence that names a tag in running text does not look like
|
|
224
|
+
# this: "<p> is the tag for a paragraph." has words after the ">".
|
|
225
|
+
HTML_LINE = %r{\A[ \t]{0,3}</?[A-Za-z][^\n]*>[ \t]*\z}
|
|
226
|
+
# The --- that opens YAML front matter. FURNITURE reads it as a
|
|
227
|
+
# horizontal rule wherever it appears; front matter is the block it
|
|
228
|
+
# opens, and only on the first line of the file.
|
|
229
|
+
FRONT = /\A---[ \t]*\z/
|
|
230
|
+
|
|
231
|
+
# The underline of a setext heading: one or more "=" for a level-1
|
|
232
|
+
# title, two or more "-" for a level-2 one. FURNITURE only knows the
|
|
233
|
+
# "#" (ATX) form. A "---" underline already drops as a horizontal
|
|
234
|
+
# rule, but the title line above it used to stay prose, and a title
|
|
235
|
+
# underlined in "=" did not drop at all.
|
|
236
|
+
SETEXT_UNDERLINE = /\A(?:=+|-{2,})[ \t]*\z/
|
|
237
|
+
|
|
238
|
+
# How many lines of YAML front matter the document opens with: the ---
|
|
239
|
+
# on the first line, everything to the next --- line, and that line. Zero
|
|
240
|
+
# for a document that does not open with one, so a --- between two
|
|
241
|
+
# paragraphs stays the horizontal rule it is.
|
|
242
|
+
def front_matter(lines)
|
|
243
|
+
return 0 unless lines.first&.chomp&.match?(FRONT)
|
|
244
|
+
|
|
245
|
+
close = lines.drop(1).index { |l| l.chomp.match?(FRONT) } or return 0
|
|
246
|
+
|
|
247
|
+
close + 2
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Both copies at once, line by line: the furniture is decided on the
|
|
251
|
+
# splitter's copy, where a code span is a placeholder, and the same lines
|
|
252
|
+
# are blanked in the copy the model is shown. Every line keeps its own
|
|
253
|
+
# length and its line ending, so an offset means the same thing in the
|
|
254
|
+
# original and in both copies. The line ending is taken off before the
|
|
255
|
+
# tests run: on a CRLF file it would otherwise sit between the line and
|
|
256
|
+
# the \z that a horizontal rule or a bare link ends at, and neither would
|
|
257
|
+
# be recognised as furniture. markdown: false still drops a line whose
|
|
258
|
+
# shape alone marks it as furniture -- FURNITURE, a list item, a list
|
|
259
|
+
# item's continuation, a setext title and its underline -- but leaves
|
|
260
|
+
# front matter, an indented code block, a lone HTML tag line and a lone
|
|
261
|
+
# code-span-or-URL line alone, because those only read as furniture
|
|
262
|
+
# once the document is read as Markdown.
|
|
263
|
+
def blank_furniture(scan, shown, markdown: true)
|
|
264
|
+
lines = scan.each_line.to_a
|
|
265
|
+
front = markdown ? front_matter(lines) : 0
|
|
266
|
+
# A setext title and its underline, marked ahead of the walk below so
|
|
267
|
+
# each line of the pair can see the other: the walk itself only ever
|
|
268
|
+
# looks at the line it is on. A line is a title when it is not blank
|
|
269
|
+
# and the next line is nothing but a setext underline; the underline
|
|
270
|
+
# is furniture only where a title sits right above it, so a "---"
|
|
271
|
+
# after a blank line is still read as the horizontal rule it is.
|
|
272
|
+
underline = lines.map { |l| l.chomp.match?(SETEXT_UNDERLINE) }
|
|
273
|
+
title = lines.each_index.map { |i| !lines[i].chomp.strip.empty? && underline[i + 1] == true }
|
|
274
|
+
item = false
|
|
275
|
+
prose = false
|
|
276
|
+
code = false
|
|
277
|
+
opens = true
|
|
278
|
+
pairs = lines.zip(shown.each_line.to_a).each_with_index.map do |(whole, also), i|
|
|
279
|
+
l = whole.chomp
|
|
280
|
+
ending = whole[l.length..]
|
|
281
|
+
blank = l.strip.empty?
|
|
282
|
+
# Only a list item runs on to the next line. An indented line under a
|
|
283
|
+
# heading, a table row, a horizontal rule or a link definition is an
|
|
284
|
+
# indented paragraph, and chaining from those dropped the prose along
|
|
285
|
+
# with the furniture above it.
|
|
286
|
+
listed = l.match?(BULLET) || ordered_item?(l, prose)
|
|
287
|
+
indented = l.match?(CODE_INDENT)
|
|
288
|
+
# An indented code block opens where a paragraph cannot be running
|
|
289
|
+
# already -- the first line, or after a blank or furniture line --
|
|
290
|
+
# and where the indent is not a list item's second line. It then runs
|
|
291
|
+
# for as long as the indent holds.
|
|
292
|
+
code = markdown && ((code && (indented || blank)) || (indented && !item && opens))
|
|
293
|
+
setext = title[i] || (i.positive? && title[i - 1])
|
|
294
|
+
dropped = i < front || code || listed || l.match?(FURNITURE) || (markdown && l.match?(HTML_LINE)) ||
|
|
295
|
+
(markdown && lone_inline?(l, prose)) || (item && l.match?(CONTINUED)) || setext
|
|
296
|
+
item = listed || (item && l.match?(CONTINUED))
|
|
297
|
+
prose = !dropped && !blank
|
|
298
|
+
opens = blank || dropped
|
|
299
|
+
dropped ? ["#{" " * l.length}#{ending}"] * 2 : [whole, also]
|
|
300
|
+
end
|
|
301
|
+
[pairs.map(&:first).join, pairs.map(&:last).join]
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# A line that is only code spans or URLs is furniture: a command on a
|
|
305
|
+
# line of its own, a bare link. With sentence punctuation after it, only
|
|
306
|
+
# where no sentence can have been running already -- after a line of
|
|
307
|
+
# prose it is the wrapped tail of that sentence, and "Run it with\n`make
|
|
308
|
+
# test`." is one sentence that would otherwise lose its object.
|
|
309
|
+
def lone_inline?(line, after_prose)
|
|
310
|
+
return true if line.match?(LONE_INLINE)
|
|
311
|
+
|
|
312
|
+
!after_prose && line.match?(LONE_INLINE_ENDED)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# CommonMark lets an ordered list interrupt a paragraph only when it
|
|
316
|
+
# starts at 1. So a hard-wrapped prose line that begins with a year --
|
|
317
|
+
# "The library was released in\n2019. It was rewritten in\n2021." -- is
|
|
318
|
+
# prose, not two list items, and the sentences on it stay in view.
|
|
319
|
+
def ordered_item?(line, after_prose)
|
|
320
|
+
m = ORDERED.match(line) or return false
|
|
321
|
+
!after_prose || m[1] == "1"
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Cutting a span out of a string by character offset walks the string from
|
|
325
|
+
# its start, because a character is not a fixed number of bytes, so cutting
|
|
326
|
+
# every paragraph of a document out of it one at a time takes the square of
|
|
327
|
+
# the document's size. The spans are asked for in the order they appear, so
|
|
328
|
+
# a cursor holds the byte offset of the last character cut at and answers
|
|
329
|
+
# each one in the length of the span itself: byteslice takes a byte offset,
|
|
330
|
+
# and a byte offset costs nothing to reach.
|
|
331
|
+
class Cursor
|
|
332
|
+
def initialize(text)
|
|
333
|
+
@text = text
|
|
334
|
+
@char = 0
|
|
335
|
+
@byte = 0
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# The span of length characters at character offset at. A cursor only
|
|
339
|
+
# goes forward: asked to go back it would answer with the wrong span,
|
|
340
|
+
# and a note would quote a string that is not in the file.
|
|
341
|
+
def cut(at, length)
|
|
342
|
+
raise ArgumentError, "a cursor at #{@char} was asked for #{at}" if at < @char
|
|
343
|
+
|
|
344
|
+
advance(at - @char)
|
|
345
|
+
advance(length)
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
private
|
|
349
|
+
|
|
350
|
+
def advance(count)
|
|
351
|
+
return "" unless count.positive?
|
|
352
|
+
|
|
353
|
+
s = window(count)
|
|
354
|
+
@char += s.length
|
|
355
|
+
@byte += s.bytesize
|
|
356
|
+
s
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
# A character is at most four bytes in UTF-8, so four bytes per character
|
|
360
|
+
# is a window wide enough to hold the ones asked for. Any other encoding
|
|
361
|
+
# widens it until it is, or until the string ends. A character split by
|
|
362
|
+
# the far edge of the window is past the count asked for, so it is never
|
|
363
|
+
# one of the ones returned.
|
|
364
|
+
def window(count)
|
|
365
|
+
width = count * 4
|
|
366
|
+
loop do
|
|
367
|
+
s = @text.byteslice(@byte, width)
|
|
368
|
+
return s[0, count] if s.length >= count || s.bytesize < width
|
|
369
|
+
|
|
370
|
+
width *= 2
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
end
|
data/lib/sloplint/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sloplint
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Benjamin Jackson
|
|
@@ -58,6 +58,7 @@ files:
|
|
|
58
58
|
- lib/sloplint/engine.rb
|
|
59
59
|
- lib/sloplint/output.rb
|
|
60
60
|
- lib/sloplint/rules.rb
|
|
61
|
+
- lib/sloplint/split.rb
|
|
61
62
|
- lib/sloplint/version.rb
|
|
62
63
|
homepage: https://github.com/benjaminjackson/sloplint
|
|
63
64
|
licenses:
|