Skip to content

Commit

Permalink
Adding support for backward compatible annotated paragraph (e.e. star…
Browse files Browse the repository at this point in the history
…ting with !!note
  • Loading branch information
Ducasse committed Sep 8, 2024
1 parent a8c7db6 commit 1835dc0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
44 changes: 44 additions & 0 deletions src/Microdown-Tests/MicrodownParserTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,54 @@ MicrodownParserTest >> testAnnotatedBlock [
root := parser parse: source.
self assert: root children size equals: 1.
annotated := root children first.
self assert: annotated class equals: MicAnnotatedParagraphBlock.
self assert: annotated label equals: 'important'.
self assert: annotated text equals: 'this is an important paragraph on one line.'


]

{ #category : 'tests - annotated-paragraph' }
MicrodownParserTest >> testAnnotatedBlockBackwardCompatible [

| source root annotated |
source := '!!important This is an important paragraph on one line.'.
root := parser parse: source.
self assert: root children size equals: 1.
annotated := root children first.
self assert: annotated class equals: MicAnnotatedParagraphBlock.
self assert: annotated label equals: 'important'.
self assert: annotated text equals: 'This is an important paragraph on one line.'


]

{ #category : 'tests - annotated-paragraph' }
MicrodownParserTest >> testAnnotatedBlockBackwardCompatibleOnLongLine [

| source root annotated |
source := '!!important This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. '.
root := parser parse: source.
self assert: root children size equals: 1.
annotated := root children first.
self assert: annotated class equals: MicAnnotatedParagraphBlock.
self assert: annotated label equals: 'important'.
self assert: annotated text equals: 'This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line. This is an important paragraph on one line.'
]

{ #category : 'tests - annotated-paragraph' }
MicrodownParserTest >> testAnnotatedBlockBackwardCompatibleWithDifferentLabel [

| source root annotated |
source := '!!remark This is an important paragraph on one line.'.
root := parser parse: source.
self assert: root children size equals: 1.
annotated := root children first.
self assert: annotated class equals: MicAnnotatedParagraphBlock.
self assert: annotated label equals: 'remark'.
self assert: annotated text equals: 'This is an important paragraph on one line.'


]

{ #category : 'tests - annotated-paragraph' }
Expand Down
48 changes: 36 additions & 12 deletions src/Microdown/MicAbstractAnnotatedBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,33 @@ Class {
{ #category : 'public' }
MicAbstractAnnotatedBlock >> addLineAndReturnNextNode: line [
"line is assumed to be of the form '!!label some text'"

isClosed
ifTrue: [ ^ self ].
label
ifNil: [
| indexOfFirstClosingBracket |
indexOfFirstClosingBracket := line indexOf: $].
label := indexOfFirstClosingBracket = 0
ifTrue: [ parent children removeLast. ^ self createMicBlockQuoteBlock: line ]
ifFalse: [line copyFrom: self lineMarkup size + 1 to: indexOfFirstClosingBracket - 1.
].
label := label trim]
ifNotNil: [ | treatedLine |
treatedLine := self extractLine: line.
body := body
ifNil: [ treatedLine ]
ifNotNil: [ body , String cr , treatedLine ]]
"this part is to be backward compatible and we will remove it in future releases"
(line beginsWith: AnnotatedParagraphBackwardCompatibleMarkup)
ifTrue: [ self handleDoubleBangStartingLine: line ]
ifFalse: [
| indexOfFirstClosingBracket |
indexOfFirstClosingBracket := line indexOf: $].
label := indexOfFirstClosingBracket = 0
ifTrue: [
parent children removeLast.
^ self createMicBlockQuoteBlock: line ]
ifFalse: [
line
copyFrom: self lineMarkup size + 1
to: indexOfFirstClosingBracket - 1 ].
label := label trim ] ]

ifNotNil: [
| treatedLine |
treatedLine := self extractLine: line.
body := body
ifNil: [ treatedLine ]
ifNotNil: [ body , String cr , treatedLine ] ]
]

{ #category : 'accessing' }
Expand Down Expand Up @@ -61,6 +72,19 @@ MicAbstractAnnotatedBlock >> extractLine: line [
^ (line copyFrom: 2 to: line size) trim
]

{ #category : 'public' }
MicAbstractAnnotatedBlock >> handleDoubleBangStartingLine: line [
"line should be in format
!!label text
"
| indexOfFirstSpace |
indexOfFirstSpace := line indexOf: Character space.
label := indexOfFirstSpace = 0
ifTrue: [ '' ]
ifFalse: [line copyFrom: '!!' size + 1 to: indexOfFirstSpace - 1.].
body := (line copyFrom: indexOfFirstSpace + 1 to: line size) trim.
]

{ #category : 'initialization' }
MicAbstractAnnotatedBlock >> initialize [

Expand Down
17 changes: 13 additions & 4 deletions src/Microdown/MicAnnotatedParagraphBlock.class.st
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
"
I represent a paragraph with the title. My title can only contain a string and my body can contain formatted text.

I'm created with `!!paragraph` (such as `@@note` `@@important` in Pillar).
I was created with `!!label` with no space `!! label` (such as `@@note` `@@important` in Pillar).

```
!!note This is important so I used exclamation marks.

```

Now since Pillar 10.3 we used `>![` since it is closer to what Github uses.

```
>![ note ]
> This is important so I used exclamation marks.
```


### Known Limitations
We should revisit the implementation because there is something unclear.
For example, we cannot currently add lists inside an annotatedBlock.
See [https://github.com/pillar-markup/MicroDown/issues/54](https://github.com/pillar-markup/MicroDown/issues/54)
The abstract class superclass does not look necessary.
"
Class {
#name : 'MicAnnotatedParagraphBlock',
Expand Down
3 changes: 3 additions & 0 deletions src/Microdown/MicMicrodownSharedPool.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Class {
'AnchorMarkup',
'AnchorReferenceCloserMarkup',
'AnchorReferenceOpenerMarkup',
'AnnotatedParagraphBackwardCompatibleMarkup',
'AnnotatedParagraphMarkup',
'AnnotationCloserMarkup',
'AnnotationOpenerMarkup',
Expand Down Expand Up @@ -57,6 +58,8 @@ MicMicrodownSharedPool class >> initialize [
"self initialize"

AnchorMarkup := '@'.
AnnotatedParagraphBackwardCompatibleMarkup := '!!'.
"we keep it to avoid breaking existing text but we should favor one that is compatible with github eg the next one"
AnnotatedParagraphMarkup := '>[!'.
CodeblockMarkup := '```'.
CommentedLineMarkup := '%'.
Expand Down
1 change: 1 addition & 0 deletions src/Microdown/MicrodownParser.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ MicrodownParser >> initialize [
dispatchTable at: HeaderMarkup put: MicHeaderBlock.
dispatchTable at: CodeblockMarkup put: MicAbstractCodeBlock.
dispatchTable at: AnnotatedParagraphMarkup put: MicAnnotatedParagraphBlock.
dispatchTable at: AnnotatedParagraphBackwardCompatibleMarkup put: MicAnnotatedParagraphBlock.
dispatchTable at: CommentedLineMarkup put: MicCommentBlock.
dispatchTable at: HorizontalLineMarkup put: MicHorizontalLineBlock.
dispatchTable at: MathOpeningBlockMarkup put: MicMathBlock.
Expand Down

0 comments on commit 1835dc0

Please sign in to comment.