diff --git a/src/Microdown-Rules/MicFigSectTableResult.class.st b/src/Microdown-Rules/MicFigSectTableResult.class.st new file mode 100644 index 00000000..347217ac --- /dev/null +++ b/src/Microdown-Rules/MicFigSectTableResult.class.st @@ -0,0 +1,32 @@ +Class { + #name : 'MicFigSectTableResult', + #superclass : 'MicAbstractResult', + #instVars : [ + 'detail' + ], + #category : 'Microdown-Rules-References', + #package : 'Microdown-Rules', + #tag : 'References' +} + +{ #category : 'accessing' } +MicFigSectTableResult >> detail [ + + ^ detail +] + +{ #category : 'accessing' } +MicFigSectTableResult >> detail: anObject [ + + detail := anObject +] + +{ #category : 'accessing' } +MicFigSectTableResult >> explanation [ + ^ 'Text: "' , micElement bodyString + , '" in file' , fileReference fullName + , ' contains a mistake: [' + , detail + , '] should be capitalized as [' + , detail capitalized , '].' +] diff --git a/src/Microdown-Rules/MicReferencesChecker.class.st b/src/Microdown-Rules/MicReferencesChecker.class.st new file mode 100644 index 00000000..75e6ad98 --- /dev/null +++ b/src/Microdown-Rules/MicReferencesChecker.class.st @@ -0,0 +1,74 @@ +" +I check that references to figures, sections and tables are correctly capitalized before an anchor. +In Microdown, references should be written as: + Figure *@figureExample@* + Section *@sectionExample@* + Table *@tableExample* + +The following are incorrect and will be flagged: + figure *@figureExample@* (lowercase) + the figure *@figureExample@* (proceded by 'the') + section *@sectionExample@* (lowercase) +" +Class { + #name : 'MicReferencesChecker', + #superclass : 'MicChecker', + #category : 'Microdown-Rules-References', + #package : 'Microdown-Rules', + #tag : 'References' +} + +{ #category : 'visiting' } +MicReferencesChecker >> visitParagraph: aMicParagraph [ + | children | + children := aMicParagraph children. + 1 to: children size - 1 do: [ :index | + | child nextChild | + child := children at: index. + nextChild := children at: index + 1. + (nextChild isKindOf: MicAnchorReferenceBlock ) ifTrue: [ + (child isKindOf: MicTextBlock ) ifTrue: [ + | words lastWord foundThe | + words := child bodyString substrings. + words isEmpty ifFalse:[ + lastWord := words last. + foundThe := false. + "verify the figure/section/table" + #('figure' 'section' 'table') do: [ :ref | + (lastWord asLowercase = ref) ifTrue: [ + words size > 1 ifTrue: [ + | prevWord | + prevWord := words at: words size - 1. + (prevWord asLowercase = 'the') ifTrue: [ + foundThe := true. + results add: (MicFigSectTableResult new + micElement: child; + inFile: aMicParagraph fromFile; + detail: 'the ' , lastWord; + yourself + ) + ] + ] + ] + + ]. + "check figure/section/table" + foundThe ifFalse: [ + #('figure' 'table' 'section') do: [ :ref | + (lastWord = ref) ifTrue: [ + results add: (MicFigSectTableResult new + micElement: child; + inFile: aMicParagraph fromFile; + detail: lastWord; + yourself + ) + ] + ] + + + ] + ] + ] + ] + ] +] diff --git a/src/Microdown-Rules/MicReferencesCheckerTest.class.st b/src/Microdown-Rules/MicReferencesCheckerTest.class.st new file mode 100644 index 00000000..5df353dd --- /dev/null +++ b/src/Microdown-Rules/MicReferencesCheckerTest.class.st @@ -0,0 +1,155 @@ +Class { + #name : 'MicReferencesCheckerTest', + #superclass : 'TestCase', + #instVars : [ + 'fileSystem', + 'checker' + ], + #category : 'Microdown-Rules-References', + #package : 'Microdown-Rules', + #tag : 'References' +} + +{ #category : 'running' } +MicReferencesCheckerTest >> generateFilesystemExample [ + | file | + file := fileSystem workingDirectory / 'figureWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'figure *@figbla@*' + ]. + file := fileSystem workingDirectory / 'tableWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'table *@tablab@*' + ]. + file := fileSystem workingDirectory / 'sectionWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'section *@secbla@*' + ]. + file := fileSystem workingDirectory / 'theFigureWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'the figure *@figbla@*' + ]. + file := fileSystem workingDirectory / 'theSectionWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'the section *@seclab@*' + ]. + file := fileSystem workingDirectory / 'theTableWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'the table *@tabbla@*' + ]. + file := fileSystem workingDirectory / 'figureCorrect.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'Figure *@figbla@*' + ]. + file := fileSystem workingDirectory / 'sectionCorrect.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'Section *@secbla@*' + ]. + file := fileSystem workingDirectory / 'tableCorrect.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'Table *@tabbla@*' + ]. + file := fileSystem workingDirectory / 'noAnchor.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: 'figure without anchor' + ]. +] + +{ #category : 'running' } +MicReferencesCheckerTest >> setUp [ + super setUp. + + fileSystem := FileSystem memory. + self generateFilesystemExample. + checker := MicReferencesChecker new. +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testFigureCorrect [ + checker checkProject: fileSystem / 'figureCorrect.md'. + self assert: checker isOkay. + + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testFigureExplanation [ + checker checkProject: fileSystem / 'figureWrong.md'. + self assert: checker results first explanation + equals: 'Text: "figure " in file/figureWrong.md contains a mistake: [figure] should be capitalized as [Figure].'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testFigureLowercaseDetected [ + checker checkProject: fileSystem / 'figureWrong.md'. + self deny: checker isOkay. + self assert: checker results size equals: 1. + self assert: checker results first detail equals: 'figure'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testSectionCorrect [ + checker checkProject: fileSystem / 'sectionCorrect.md'. + self assert: checker isOkay. + + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testSectionLowercaseDetected [ + checker checkProject: fileSystem / 'sectionWrong.md'. + self deny: checker isOkay. + self assert: checker results size equals: 1. + self assert: checker results first detail equals: 'section'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTableCorrect [ + checker checkProject: fileSystem / 'tableCorrect.md'. + self assert: checker isOkay. + + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTableLowercaseDetected [ + checker checkProject: fileSystem / 'tableWrong.md'. + self deny: checker isOkay. + self assert: checker results size equals: 1. + self assert: checker results first detail equals: 'table'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTextWithoutAnchor [ + checker checkProject: fileSystem / 'noAnchor.md'. + self assert: checker isOkay. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTheFigureDetected [ + checker checkProject: fileSystem / 'theFigureWrong.md'. + self deny: checker isOkay. + self assert: checker results first detail equals: 'the figure'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTheSectionDetected [ + checker checkProject: fileSystem / 'theSectionWrong.md'. + self deny: checker isOkay. + self assert: checker results first detail equals: 'the section'. + +] + +{ #category : 'running' } +MicReferencesCheckerTest >> testTheTableDetected [ + checker checkProject: fileSystem / 'theTableWrong.md'. + self deny: checker isOkay. + self assert: checker results first detail equals: 'the table'. + +]