Skip to content

Commit

Permalink
Fixed references
Browse files Browse the repository at this point in the history
  • Loading branch information
Ducasse committed May 23, 2024
1 parent 24c0d76 commit 4fa59cb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 80 deletions.
45 changes: 16 additions & 29 deletions src/Microdown-ReferenceChecker/MicReferenceChecker.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ Class {
#package : 'Microdown-ReferenceChecker'
}

{ #category : 'visiting' }
MicReferenceChecker >> attachFile: anElement [

"get the file from the root of the parsed tree to add it along the anchors later so we know in what file are the anchors or elements that caused the problem , what worth noting here is in the cases where anchor is not an object that will not work "

|fakeElement|
fakeElement := anElement .
(anElement isKindOf: MicRootBlock )
ifTrue: [
^ (fakeElement propertyAt: #file ifAbsent: [ ]) ]
ifFalse: [
fakeElement := fakeElement parent .
self attachFile: fakeElement
]

]

{ #category : 'visiting' }
MicReferenceChecker >> checkDirectory: aDir [
"Take the directory, parse all its children with microdown file parser and let the visitor visit each time then return visitor is ok which should be true if every thing is okay, the visitor turned out to treat the many documents that it visits as one, so if anchor is duplicated in another file it will detect that . "
Expand All @@ -41,7 +24,6 @@ MicReferenceChecker >> checkDirectory: aDir [
aDir allFiles do: [ :each |
(parsedFile := Microdown parseFile: each) accept: self.
].

^ self isOk

]
Expand Down Expand Up @@ -69,7 +51,6 @@ MicReferenceChecker >> duplicatedAnchors [
MicReferenceChecker >> handleAnchorOf: anElement [

anElement hasAnchor ifFalse: [ ^ self ].

(self hasAlreadyDefinedAs: anElement)
ifTrue: [ duplicatedAnchors add: anElement ].
anchors add: anElement
Expand All @@ -79,9 +60,12 @@ MicReferenceChecker >> handleAnchorOf: anElement [
{ #category : 'visiting' }
MicReferenceChecker >> hasAlreadyDefinedAs: anAnchor [

^ (anchors
detect: [ :each | each label = anAnchor anchorLabel ]
ifNone: [ #( ) ]) isNotEmpty
| alreadyDefined |
alreadyDefined := false.
anchors do:
[ :each | each anchorLabel = anAnchor anchorLabel
ifTrue: [ alreadyDefined := true ] ].
^ alreadyDefined
]

{ #category : 'initialization' }
Expand All @@ -97,17 +81,20 @@ MicReferenceChecker >> initialize [
MicReferenceChecker >> isOk [

^ duplicatedAnchors isEmpty and: [
references allSatisfy: [ :each | anchors includes: each ] ]
references allSatisfy: [ :each | self hasAlreadyDefinedAs: each ] ]
]

{ #category : 'reporting' }
MicReferenceChecker >> unknownAnchors [

| unk |
unk := references copy.
anchors do: [ :each |
unk remove: each ifAbsent: [ ] ].
^ unk
| unknown ref |
unknown := OrderedCollection new.
ref := references copy.
ref do: [ :ref |
(anchors noneSatisfy: [ :each |
ref anchorLabel = each anchorLabel ])
ifTrue: [ unknown add: ref ] ].
^ unknown
]

{ #category : 'visiting' }
Expand All @@ -122,7 +109,7 @@ MicReferenceChecker >> visitAnchor: anAnchor [
{ #category : 'visiting' }
MicReferenceChecker >> visitAnchorReference: anAnchorReference [

references add: anAnchorReference bodyString
references add: anAnchorReference
]

{ #category : 'visiting' }
Expand Down
80 changes: 29 additions & 51 deletions src/Microdown-ReferenceChecker/MicReferenceCheckerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ See *@anchorSection1@*
MicReferenceCheckerTest >> testAllReferencesAreCorrectInFile [

| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file := (FileSystem memory / 'myFile.txt') asFileReference.
file ensureCreateFile.



file writeStreamDo: [ :stream | stream nextPutAll: '# Section 1
@anchorSection1
Expand All @@ -46,30 +43,24 @@ See *@anchorSection1@*
{ #category : 'tests' }
MicReferenceCheckerTest >> testAllReferencesAreCorrectinDir [

| dir file1 file2 visitor |
| dir file1 file2 visitor |
dir := (FileSystem workingDirectory / 'myDirectory') asFileReference.
dir ensureCreateDirectory .

dir ensureCreateDirectory.
file1 := (FileSystem workingDirectory / 'myDirectory' / 'file1.txt') asFileReference.
file1 writeStreamDo: [ :stream | stream nextPutAll: '# Section
@anchorSection0
# Section 1
@anchorSection1
' ] .
file1 ensureCreateFile .

file2 := (FileSystem workingDirectory / 'myDirectory' / 'file2.txt') asFileReference.
file2 writeStreamDo: [ :stream | stream nextPutAll: ' See *@anchorSection1@* and *@anchorSection1@*'] .
file2 ensureCreateFile .
file2 ensureCreateFile.

visitor := MicReferenceChecker new.
self assert:( visitor checkDirectory: dir ) .
file1 ensureDelete.
file2 ensureDelete.
dir ensureDelete
self assert: (visitor checkDirectory: dir).

]

Expand Down Expand Up @@ -112,21 +103,16 @@ MicReferenceCheckerTest >> testDuplicatedAnchorDir [
MicReferenceCheckerTest >> testFile [

| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file := (FileSystem memory / 'myFile.txt') asFileReference.
file ensureCreateFile.



file writeStreamDo: [ :stream | stream nextPutAll: '# Section 1
![alittle caption.](figures/f.png anchor=anchorSection1)
See *@anchorSection0@*
' ] .


' ].
visitor := MicReferenceChecker new.
self deny: (visitor checkFile: file )
self deny: (visitor checkFile: file)
]

{ #category : 'tests' }
Expand All @@ -148,20 +134,16 @@ See *@anchorSection1@*
MicReferenceCheckerTest >> testReferToAFigureInFile [

| file visitor |
file := (FileSystem workingDirectory / 'myFile2.txt') asFileReference.
file := (FileSystem memory / 'myFile2.txt') asFileReference.
file ensureCreateFile.



file writeStreamDo: [ :stream | stream nextPutAll: '# Section 1
![alittle caption.](figures/f.png anchor=anchorSection1)
See *@anchorSection1@*
' ] .
' ].
visitor := MicReferenceChecker new.
self assert: (visitor checkFile: file ).
file ensureDelete
self assert: (visitor checkFile: file).
]

{ #category : 'tests' }
Expand All @@ -185,7 +167,7 @@ See *@anchorSection1@*
MicReferenceCheckerTest >> testReferToAMathEquationInFile [

| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file := (FileSystem memory / 'myFile.txt') asFileReference.
file ensureCreateFile.


Expand Down Expand Up @@ -224,7 +206,7 @@ See *@anchorSection1@*
MicReferenceCheckerTest >> testReferToAnUknownAnchorInFile [

| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file := (FileSystem memory / 'myFile.txt') asFileReference.
file ensureCreateFile.


Expand Down Expand Up @@ -263,7 +245,7 @@ See *@anchorSection1@* and *@anchorSection0@*
visitor := MicReferenceChecker new.
doc accept: visitor.
self deny: visitor isOk.
self assert: visitor duplicatedAnchors equals: OrderedCollection <- #('anchorSection1' 'anchorSection1')
self assert: (visitor duplicatedAnchors collect: [:each | each anchorLabel ]) equals: OrderedCollection <- #('anchorSection1' 'anchorSection1')
]

{ #category : 'tests - duplicated anchors' }
Expand Down Expand Up @@ -294,8 +276,10 @@ See *@anchorSection1@* and *@anchorSection0@*

visitor := MicReferenceChecker new.
self deny: (visitor checkFile: file).
self assert: visitor duplicatedAnchors equals: OrderedCollection <- #('anchorSection1' 'anchorSection1') .
file ensureDelete
self
assert: (visitor duplicatedAnchors collect: [:each | each anchorLabel])
equals: OrderedCollection <- #('anchorSection1' 'anchorSection1').
file ensureDelete
]

{ #category : 'tests - duplicated anchors' }
Expand All @@ -318,7 +302,7 @@ See *@anchorSection1@* and *@anchorSection3@*
doc accept: visitor.
self deny: visitor isOk.
self
assert: visitor duplicatedAnchors
assert: (visitor duplicatedAnchors collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'anchorSection1' )
]

Expand All @@ -328,9 +312,6 @@ MicReferenceCheckerTest >> testReportingDuplicatedFiguresInFile [
| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file ensureCreateFile.



file writeStreamDo: [ :stream | stream nextPutAll: '# Section
@anchorSection0
Expand All @@ -342,14 +323,12 @@ MicReferenceCheckerTest >> testReportingDuplicatedFiguresInFile [
See *@anchorSection1@* and *@anchorSection3@*
'] .
.
'].
visitor := MicReferenceChecker new.
self deny: (visitor checkFile: file) .
self deny: (visitor checkFile: file).
self
assert: visitor duplicatedAnchors
equals: OrderedCollection <- #( 'anchorSection1' ) .

assert: (visitor duplicatedAnchors collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'anchorSection1' ).
file ensureDelete
]

Expand Down Expand Up @@ -383,7 +362,7 @@ See *@anchorSection1@* and *@anchorSection3@*
doc accept: visitor.
self deny: visitor isOk.
self
assert: visitor duplicatedAnchors
assert: (visitor duplicatedAnchors collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'anchorSection1' 'anchorSection1' )
]

Expand All @@ -393,9 +372,6 @@ MicReferenceCheckerTest >> testReportingDuplicatedMathsInFile [
| file visitor |
file := (FileSystem workingDirectory / 'myFile.txt') asFileReference.
file ensureCreateFile.



file writeStreamDo: [ :stream | stream nextPutAll: '# Section
@anchorSection0
Expand All @@ -420,9 +396,9 @@ See *@anchorSection1@* and *@anchorSection3@*
'] .

visitor := MicReferenceChecker new.
self deny: (visitor checkFile: file) .
self deny: (visitor checkFile: file).
self
assert: visitor duplicatedAnchors
assert: (visitor duplicatedAnchors collect: [ :each | each anchorLabel ])
equals: OrderedCollection <- #( 'anchorSection1' 'anchorSection1' ).
file ensureDelete
]
Expand All @@ -443,7 +419,9 @@ See *@anchorSection1@* and *@anchorSection2@*
visitor := MicReferenceChecker new.
doc accept: visitor.
self deny: visitor isOk.
self assert: visitor unknownAnchors equals: (OrderedCollection <- #('anchorSection2'))
self
assert: (visitor unknownAnchors collect: [ :each | each anchorLabel ])
equals: (OrderedCollection <- #('anchorSection2'))
]

{ #category : 'tests' }
Expand Down
8 changes: 8 additions & 0 deletions src/Microdown/MicAnchorBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ MicAnchorBlock >> label: aLabel [

]

{ #category : 'printing' }
MicAnchorBlock >> printOn: aStream [

super printOn: aStream.
self hasLabel
ifTrue: [ aStream nextPutAll: ' (', self label, ')' ]
]

{ #category : 'accessing' }
MicAnchorBlock >> target [
^ target
Expand Down
6 changes: 6 additions & 0 deletions src/Microdown/MicAnchorReferenceBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ MicAnchorReferenceBlock >> accept: aVisitor [
^ aVisitor visitAnchorReference: self
]

{ #category : 'accessing' }
MicAnchorReferenceBlock >> anchorLabel [

^ bodyString
]

{ #category : 'accessing' }
MicAnchorReferenceBlock >> reference [

Expand Down

0 comments on commit 4fa59cb

Please sign in to comment.