Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a new hook to support the contextual redefinition of line m… #912

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Microdown-BookTester/MicCodeBlockValidator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ MicCodeBlockValidator >> validTests [
MicCodeBlockValidator >> visitCode: aCodeBlock [
"Creates an instance of PRBookTestResult with parameters depending of the type of the codeblock."


(strategies keys includes: aCodeBlock mainExtensionTag)
ifFalse: [ ^ self ].

Expand Down
60 changes: 52 additions & 8 deletions src/Microdown-Tests/MicCodeBlockTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,25 @@ MicCodeBlockTest >> testCodeBlockWithNestedCodeBlock [
"This test shows that we can write a code block inside a code block but that the later is not interpreter."

| source root code textBody |
textBody := ' ```
my fancy code
is really cool
```'.
source := CodeblockMarkup, String cr, textBody, String cr, CodeblockMarkup.
source := '```
This is a first level code
```
this is one with tabs
```

```
this is one with space
```
``` '.

root := parser parse: source.

self
assert: root children size
equals: 1.
code := root children first.
self assert: code children isEmpty.
self
assert: code code
equals: textBody.
self assert: code class equals: MicCodeBlock

]

Expand Down Expand Up @@ -355,6 +359,30 @@ Color >> asMorph
self assert: code mainExtensionTag equals: #caption
]

{ #category : 'tests' }
MicCodeBlockTest >> testMainExtensionTag2 [
"
```example=true
3 + 7
>> 10
```
"

| source root code textBody argument |
textBody := '3 + 7
>>> 10'.
argument := 'example=true'.
source := CodeblockMarkup , argument , String cr , textBody
, String cr , CodeblockMarkup.
root := self parser parse: source.
self assert: root children size equals: 1.
code := root children first.
self assert: code code equals: textBody.
self assert: (code arguments at: #language) equals: 'Pharo'.
self assert: code mainExtensionTag equals: #example.

]

{ #category : 'tests - arguments' }
MicCodeBlockTest >> testMainExtensionTagForSync [

Expand Down Expand Up @@ -436,6 +464,22 @@ Color >> asMorph
self assert: code mainExtensionTag equals: #caption
]

{ #category : 'tests - code' }
MicCodeBlockTest >> testNestedCodeBlockOnlyProducesOneCodeBlock [

| doc |
doc := Microdown parse: '```
```example=true&expectedFailure=true
3 + 4
>>> 12
```
```'.

self assert: doc children size equals: 1.
self assert: doc children first class equals: MicCodeBlock.

]

{ #category : 'tests' }
MicCodeBlockTest >> testOpenCanConsumeLine [
| source root textBody argument line code |
Expand Down
7 changes: 7 additions & 0 deletions src/Microdown/MicAbstractBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ MicAbstractBlock >> listItemBlockClass [
^ MicListItemBlock
]

{ #category : 'hooks' }
MicAbstractBlock >> massageLine: aLine [
"In some cases an element may want to massage the line such as removing leading tab. By default we delegate to the parent because environment may contain nodes such as code block and this is this nesting that will determine what should be done with the tab. In such a case the environment should remov the tabs."

^ self parent massageLine: aLine
]

{ #category : 'public' }
MicAbstractBlock >> nestedLevel [
"Return the nesting level of main blocks. Basically only list increases this."
Expand Down
9 changes: 9 additions & 0 deletions src/Microdown/MicEnvironmentBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ MicEnvironmentBlock >> addLineAndReturnNextNode: line [
"add line to this node.
Notice, the action is allowed to create new nodes in the block tree.
Returns the node to handle next line - typically self."


| withoutPreTabs |
isClosed
ifTrue: [ ^ self ].
withoutPreTabs := line withoutPreTabs.
"Here the withoutPreTabs is probably not necessary because handlingPrefixTabOfLine: should do its job."
(self doesLineStartWithStopMarkup: withoutPreTabs)
ifTrue: [ ^ self ].
firstLine
Expand Down Expand Up @@ -195,3 +198,9 @@ MicEnvironmentBlock >> lineStopMarkup [

^ EnvironmentClosingBlockMarkup
]

{ #category : 'handle' }
MicEnvironmentBlock >> massageLine: aLine [

^ aLine withoutPreTabs
]
7 changes: 7 additions & 0 deletions src/Microdown/MicRootBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ MicRootBlock >> indent [
^0
]

{ #category : 'hooks' }
MicRootBlock >> massageLine: aLine [
"In some cases an element may want to massage the line such as removing leading tab. By default we delegate to the parent because environments may contain nodes such as code block and this is this nesting that will determine what should be done with the tab. In such a case the environment should remov the tabs. Here on the root we do nothing special."

^ aLine
]

{ #category : 'testing' }
MicRootBlock >> metaDataElement [
"if self hasMetaDataElement then metaDataElement returns it.
Expand Down
2 changes: 1 addition & 1 deletion src/Microdown/MicStartStopMarkupBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ MicStartStopMarkupBlock >> canConsumeLine: line [

{ #category : 'testing' }
MicStartStopMarkupBlock >> doesLineStartWithStopMarkup: line [

"return if the line starts with a stop markup"
^ line beginsWith: self lineStopMarkup
]
Expand Down
2 changes: 1 addition & 1 deletion src/Microdown/MicrodownParser.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ MicrodownParser >> handleLine: line [
-if the current block can consume the line, it manages it and this potentially creates a new block that becomes the current one.
When the line is not consumed, the current block is closed and its parent becomes the current one and the process is called back to treat the line."

(current canConsumeLine: (line withoutPreTabs))
(current canConsumeLine: (current massageLine: line))
ifTrue: [
current := current addLineAndReturnNextNode: line.
^ current ]
Expand Down
Loading