From 870778cf3c19eaa9353575b11a5614518b76f333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Sat, 25 May 2024 18:48:49 +0200 Subject: [PATCH 1/3] started to get green tests on supporting ![ a caption % width=60&anchor=mylabel](figures/f.png) to support annotated picture display on github and friends --- .../MicFigureBlockTest.class.st | 45 +++++++++++++++++++ src/Microdown/MicInlineBlockWithUrl.class.st | 7 +-- src/Microdown/MicInlineCommentBlock.class.st | 25 +++++++++++ src/Microdown/MicInlineParser.class.st | 24 +++++++--- .../MicInlineStandardDelimiter.class.st | 3 ++ 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 src/Microdown/MicInlineCommentBlock.class.st diff --git a/src/Microdown-Tests/MicFigureBlockTest.class.st b/src/Microdown-Tests/MicFigureBlockTest.class.st index eb827870..b1277e90 100644 --- a/src/Microdown-Tests/MicFigureBlockTest.class.st +++ b/src/Microdown-Tests/MicFigureBlockTest.class.st @@ -61,6 +61,19 @@ MicFigureBlockTest >> testAnchorExpressedUsingLabel [ self assert: figure anchor equals: '233' ] +{ #category : 'tests - new position' } +MicFigureBlockTest >> testAnchorInCaption [ + + | figure | + figure := (splitterClass new parse: + '![ caption with space and dot... % width=80&anchor=233]( https://github.com/pillar-markup/Microdown/runs/4336482137?check_suite_focus=true )') first. + + self assert: figure hasArguments. + self assert: figure hasAnchor. + self assert: figure anchor equals: '233'. + self assert: (figure arguments at: #width) equals: '80' +] + { #category : 'running' } MicFigureBlockTest >> testArgumentAtIfPresent [ @@ -113,6 +126,24 @@ MicFigureBlockTest >> testFigureCaptionIsReified [ self assert: fig children second parent equals: fig ] +{ #category : 'tests - new position' } +MicFigureBlockTest >> testFigureCaptionIsReifiedWithNewFormat [ + "And as such their parent is the math block" + + | root fig | + root := Microdown parse: ' +![This **great** caption % width=666&anchor=myAnchor](figures/f.png)'. + fig := root children first children first. + self + assert: fig children second class equals: MicBoldFormatBlock. + self assert: fig children second parent equals: fig. + + self assert: fig hasArguments. + self assert: fig hasAnchor. + self assert: fig anchor equals: 'myAnchor'. + self assert: (fig arguments at: #width) equals: '666' +] + { #category : 'tests' } MicFigureBlockTest >> testHasArguments [ | figure | @@ -137,6 +168,20 @@ MicFigureBlockTest >> testHasNoArguments [ ] +{ #category : 'tests - new position' } +MicFigureBlockTest >> testIfArgumentInCaptionIgnoresInURL [ + "Since we will move to the first solution" + + | figure | + figure := (splitterClass new parse: + '![ caption with space and dot... % width=80&anchor=233]( https://github.com/pillar-markup/Microdown/runs/4336482137?check_suite_focus=true width=100&anchor=NotThatOne)') first. + + self assert: figure hasArguments. + self assert: figure hasAnchor. + self assert: figure anchor equals: '233'. + self assert: (figure arguments at: #width) equals: '80' +] + { #category : 'tests - testing' } MicFigureBlockTest >> testIsNotRelativeFilePathWithUrl [ | figure | diff --git a/src/Microdown/MicInlineBlockWithUrl.class.st b/src/Microdown/MicInlineBlockWithUrl.class.st index 92787615..bf577f6c 100644 --- a/src/Microdown/MicInlineBlockWithUrl.class.st +++ b/src/Microdown/MicInlineBlockWithUrl.class.st @@ -119,9 +119,10 @@ MicInlineBlockWithUrl >> closeMe [ | split title | split := url splitOnFirst: Character space. self reference: (MicResourceReference fromUri: split first). - title := (split second ifNil: [ '' ]) - trimBoth: [:char | {$". Character space} includes: char]. - self arguments: (MicArgumentList withString: title) + self arguments ifNil: [ + title := (split second ifNil: [ '' ]) + trimBoth: [:char | {$". Character space} includes: char]. + self arguments: (MicArgumentList withString: title)] ] { #category : 'utilities' } diff --git a/src/Microdown/MicInlineCommentBlock.class.st b/src/Microdown/MicInlineCommentBlock.class.st new file mode 100644 index 00000000..c88d588c --- /dev/null +++ b/src/Microdown/MicInlineCommentBlock.class.st @@ -0,0 +1,25 @@ +Class { + #name : 'MicInlineCommentBlock', + #superclass : 'MicUnEvaluatedBlock', + #category : 'Microdown-Model', + #package : 'Microdown', + #tag : 'Model' +} + +{ #category : 'accessing' } +MicInlineCommentBlock class >> closingDelimiter [ + + ^ self shouldNotImplement +] + +{ #category : 'accessing' } +MicInlineCommentBlock class >> openingDelimiter [ + + ^ CommentedLineMarkup +] + +{ #category : 'visiting' } +MicInlineCommentBlock >> accept: aVisitor [ + "For now do nothing" + +] diff --git a/src/Microdown/MicInlineParser.class.st b/src/Microdown/MicInlineParser.class.st index 056c0335..473b4251 100644 --- a/src/Microdown/MicInlineParser.class.st +++ b/src/Microdown/MicInlineParser.class.st @@ -98,15 +98,29 @@ MicInlineParser >> parseEvaluatedBlock: blockType token: token stream: tokenStre { #category : 'private parsing' } MicInlineParser >> parseNameUrlBlock: blockType from: aTokenStream token: token [ - | skipRes children urlToken | + | skipRes children urlToken captionAndArgs caption rest toCaption newBlock | + skipRes := self skipToUrlStartInStream: aTokenStream. skipRes ifNil: [ ^ self createTextBlock: token string ]. - children := self parseChildrenIn: skipRes second. + + "We look if we have a % in the caption to remove the rest from the caption + and place them as arguments" + caption := OrderedCollection new. + rest := OrderedCollection new. + toCaption := true. + skipRes second contents do: [:each | + each delimiter markup = '%' + ifTrue: [toCaption := false]. + toCaption ifTrue: [ caption add: each ] + ifFalse: [ rest add: each ] ]. + children := self parseChildrenIn: caption readStream. urlToken := aTokenStream next. - ^ blockType new + newBlock := blockType new children: children; - url: urlToken undelimitedSubstring; - closeMe + url: urlToken undelimitedSubstring. + rest ifNotEmpty: [ newBlock arguments: (MicArgumentList withString: rest second string )]. + newBlock closeMe. + ^ newBlock ] { #category : 'private parsing' } diff --git a/src/Microdown/MicInlineStandardDelimiter.class.st b/src/Microdown/MicInlineStandardDelimiter.class.st index 02fa975a..b28602bb 100644 --- a/src/Microdown/MicInlineStandardDelimiter.class.st +++ b/src/Microdown/MicInlineStandardDelimiter.class.st @@ -23,6 +23,9 @@ MicInlineStandardDelimiter class >> initializeDelimiters [ "The fixious text delimiter" self new markup: FixiousTextDelimiter; blockClass: MicTextBlock; closer: nil; addMe. + "The comment text delimiter" + self new markup: CommentedLineMarkup ; blockClass: MicInlineCommentBlock; closer: nil; addMe. + "formating" self new markup: BoldMarkup; blockClass: MicBoldFormatBlock; closer: BoldMarkup; addMe. self new markup: ItalicMarkup; blockClass: MicItalicFormatBlock; closer: ItalicMarkup; addMe. From 1efa9ef6f8f1caa84eaca35c02d2bdf51a15290e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Sat, 25 May 2024 19:11:13 +0200 Subject: [PATCH 2/3] For remi --- src/Microdown-Tests/MicFigureBlockTest.class.st | 3 ++- src/Microdown/MicInlineParser.class.st | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Microdown-Tests/MicFigureBlockTest.class.st b/src/Microdown-Tests/MicFigureBlockTest.class.st index b1277e90..3eee0838 100644 --- a/src/Microdown-Tests/MicFigureBlockTest.class.st +++ b/src/Microdown-Tests/MicFigureBlockTest.class.st @@ -71,7 +71,8 @@ MicFigureBlockTest >> testAnchorInCaption [ self assert: figure hasArguments. self assert: figure hasAnchor. self assert: figure anchor equals: '233'. - self assert: (figure arguments at: #width) equals: '80' + self assert: (figure arguments at: #width) equals: '80'. + self assert: figure children first bodyString equals: 'caption with space and dot...' ] { #category : 'running' } diff --git a/src/Microdown/MicInlineParser.class.st b/src/Microdown/MicInlineParser.class.st index 473b4251..cb39df55 100644 --- a/src/Microdown/MicInlineParser.class.st +++ b/src/Microdown/MicInlineParser.class.st @@ -98,8 +98,8 @@ MicInlineParser >> parseEvaluatedBlock: blockType token: token stream: tokenStre { #category : 'private parsing' } MicInlineParser >> parseNameUrlBlock: blockType from: aTokenStream token: token [ - | skipRes children urlToken captionAndArgs caption rest toCaption newBlock | - + | skipRes children urlToken caption rest toCaption newBlock | + self halt. skipRes := self skipToUrlStartInStream: aTokenStream. skipRes ifNil: [ ^ self createTextBlock: token string ]. @@ -113,12 +113,18 @@ MicInlineParser >> parseNameUrlBlock: blockType from: aTokenStream token: token ifTrue: [toCaption := false]. toCaption ifTrue: [ caption add: each ] ifFalse: [ rest add: each ] ]. + self halt. children := self parseChildrenIn: caption readStream. + self halt. urlToken := aTokenStream next. newBlock := blockType new children: children; url: urlToken undelimitedSubstring. - rest ifNotEmpty: [ newBlock arguments: (MicArgumentList withString: rest second string )]. + rest + ifNotEmpty: [ newBlock arguments: (MicArgumentList withString: rest second string )] + ifEmpty: [ + "We mark it to nil to indicate to the closeMe that there were no arguments in the caption. In the future when all the books and more will not use arguments in the url then we will remove this hack. For now it enables backward compatibility while giving precedence to caption arguments." + newBlock arguments: nil ]. newBlock closeMe. ^ newBlock ] From db8aa425f86081f2220acdf3ab69cbc720449725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Sat, 25 May 2024 21:13:37 +0200 Subject: [PATCH 3/3] use stream iteration instead of contents iteration. --- src/Microdown-Tests/MicFigureBlockTest.class.st | 2 +- src/Microdown/MicInlineParser.class.st | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Microdown-Tests/MicFigureBlockTest.class.st b/src/Microdown-Tests/MicFigureBlockTest.class.st index 3eee0838..c810338f 100644 --- a/src/Microdown-Tests/MicFigureBlockTest.class.st +++ b/src/Microdown-Tests/MicFigureBlockTest.class.st @@ -72,7 +72,7 @@ MicFigureBlockTest >> testAnchorInCaption [ self assert: figure hasAnchor. self assert: figure anchor equals: '233'. self assert: (figure arguments at: #width) equals: '80'. - self assert: figure children first bodyString equals: 'caption with space and dot...' + self assert: figure children first bodyString equals: ' caption with space and dot... ' ] { #category : 'running' } diff --git a/src/Microdown/MicInlineParser.class.st b/src/Microdown/MicInlineParser.class.st index cb39df55..ef6e69c3 100644 --- a/src/Microdown/MicInlineParser.class.st +++ b/src/Microdown/MicInlineParser.class.st @@ -99,29 +99,29 @@ MicInlineParser >> parseEvaluatedBlock: blockType token: token stream: tokenStre MicInlineParser >> parseNameUrlBlock: blockType from: aTokenStream token: token [ | skipRes children urlToken caption rest toCaption newBlock | - self halt. + skipRes := self skipToUrlStartInStream: aTokenStream. skipRes ifNil: [ ^ self createTextBlock: token string ]. - "We look if we have a % in the caption to remove the rest from the caption and place them as arguments" caption := OrderedCollection new. rest := OrderedCollection new. toCaption := true. - skipRes second contents do: [:each | + "Pay attention skipRes second do: is not equal to skip second contents do: the latter returns the complete stream ignoring previous positions." + skipRes second do: [:each | each delimiter markup = '%' ifTrue: [toCaption := false]. - toCaption ifTrue: [ caption add: each ] + toCaption + ifTrue: [ caption add: each ] ifFalse: [ rest add: each ] ]. - self halt. - children := self parseChildrenIn: caption readStream. - self halt. + caption := caption readStream. + children := self parseChildrenIn: caption. urlToken := aTokenStream next. newBlock := blockType new children: children; url: urlToken undelimitedSubstring. rest - ifNotEmpty: [ newBlock arguments: (MicArgumentList withString: rest second string )] + ifNotEmpty: [ newBlock arguments: (MicArgumentList withString: rest second string )] ifEmpty: [ "We mark it to nil to indicate to the closeMe that there were no arguments in the caption. In the future when all the books and more will not use arguments in the url then we will remove this hack. For now it enables backward compatibility while giving precedence to caption arguments." newBlock arguments: nil ].