Skip to content
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
50 changes: 50 additions & 0 deletions src/Microdown-Tests/MicSecondaryRootBlockTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Class {
#name : 'MicSecondaryRootBlockTest',
#superclass : 'TestCase',
#category : 'Microdown-Tests-Model',
#package : 'Microdown-Tests',
#tag : 'Model'
}

{ #category : 'tests' }
MicSecondaryRootBlockTest >> testAcceptDoubleDispatch [

| block visitor |
block := MicSecondaryRootBlock new.
visitor := MicrodownVisitor new.
self shouldnt: [ block accept: visitor ] raise: Error
]

{ #category : 'tests' }
MicSecondaryRootBlockTest >> testCanHoldChildren [

| root child |
root := MicSecondaryRootBlock new.
child := MicParagraphBlock new.

root addChild: child.

self assert: root children size equals: 1.
self assert: root children first equals: child
]

{ #category : 'tests' }
MicSecondaryRootBlockTest >> testFileProperty [
| root fileRef |
root := MicSecondaryRootBlock new.
fileRef := 'fichierInclus.md' asFileReference.

root fromFile: fileRef.

self assert: root fromFile equals: fileRef
]

{ #category : 'tests' }
MicSecondaryRootBlockTest >> testIsSecondaryRoot [
| mainRoot secondaryRoot |
mainRoot := MicRootBlock new.
secondaryRoot := MicSecondaryRootBlock new.

self deny: mainRoot isSecondaryRoot.
self assert: secondaryRoot isSecondaryRoot
]
127 changes: 52 additions & 75 deletions src/Microdown-Transformer-Tests/MicFileIncluderTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ Pharo is **cool**

{ #category : 'tests' }
MicFileIncluderTest >> testHandleNestedFileResolution [

| root file reference |
(filesystem workingDirectory / 'Chapters') createDirectory.
(filesystem workingDirectory / 'Chapters' / 'SUnit') createDirectory.
Expand All @@ -95,125 +94,106 @@ MicFileIncluderTest >> testHandleNestedFileResolution [
setStream: stream;
inputfile: 'SUnit/anExample1.md' ].

file := filesystem workingDirectory / 'Chapters' / 'SUnit'
/ 'anExample1.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'anExample1.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
header: [ builder text: 'Foo' ] withLevel: 1;
inputfile: 'anExample2.md' ].

file := filesystem workingDirectory / 'Chapters' / 'SUnit'
/ 'anExample2.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'anExample2.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
header: [ builder text: 'Example2' ] withLevel: 1 ].

reference := (MicResourceReference fromUri:
'file:///Chapters/SUnit.md') fileSystem: filesystem.
reference := (MicResourceReference fromUri: 'file:///Chapters/SUnit.md') fileSystem: filesystem.

includer topFile: reference fileReference parent.
includer visit: (root := reference loadMicrodown).

self assert: root children first class equals: MicHeaderBlock.
self
assert: root children first children first bodyString
equals: 'Foo'.
self assert: root children first class equals: MicSecondaryRootBlock.
self assert: root children first children first class equals: MicHeaderBlock.
self assert: root children first children first children first bodyString equals: 'Foo'.

self assert: root children second class equals: MicHeaderBlock.
self
assert: root children second children first bodyString
equals: 'Example2'
self assert: root children first children second class equals: MicSecondaryRootBlock.
self assert: root children first children second children first class equals: MicHeaderBlock.
self assert: root children first children second children first children first bodyString equals: 'Example2'
]

{ #category : 'tests' }
MicFileIncluderTest >> testHandleNestedFileResolutionOnDifferentLevel [

| root file reference |
(filesystem workingDirectory / 'Chapters') createDirectory.
(filesystem workingDirectory / 'Chapters' / 'SUnit') createDirectory.
(filesystem workingDirectory / 'Chapters' / 'SUnit' / 'Sub')
createDirectory.
(filesystem workingDirectory / 'Chapters' / 'SUnit' / 'Sub') createDirectory.

file := filesystem workingDirectory / 'Chapters' / 'SUnit.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
inputfile: 'SUnit/anExample1.md' ].

file := filesystem workingDirectory / 'Chapters' / 'SUnit'
/ 'anExample1.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'anExample1.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
header: [ builder text: 'Foo' ] withLevel: 1;
inputfile: 'Sub/anExample2.md' ].

file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'Sub'
/ 'anExample2.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'Sub' / 'anExample2.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
header: [ builder text: 'Example2' ] withLevel: 1 ].

reference := (MicResourceReference fromUri:
'file:///Chapters/SUnit.md') fileSystem: filesystem.
reference := (MicResourceReference fromUri: 'file:///Chapters/SUnit.md') fileSystem: filesystem.

includer topFile: reference fileReference parent.
includer visit: (root := reference loadMicrodown).

self assert: root children first class equals: MicHeaderBlock.
self
assert: root children first children first bodyString
equals: 'Foo'.
self assert: root children first class equals: MicSecondaryRootBlock.
self assert: root children first children first class equals: MicHeaderBlock.
self assert: root children first children first children first bodyString equals: 'Foo'.

self assert: root children second class equals: MicHeaderBlock.
self
assert: root children second children first bodyString
equals: 'Example2'
self assert: root children first children second class equals: MicSecondaryRootBlock.
self assert: root children first children second children first class equals: MicHeaderBlock.
self assert: root children first children second children first children first bodyString equals: 'Example2'
]

{ #category : 'tests' }
MicFileIncluderTest >> testIncluderIncludesInputFileFromTheSameLevel [

| root file reference |
(filesystem workingDirectory / 'Chapters') createDirectory.
(filesystem workingDirectory / 'Chapters' / 'SUnit') createDirectory.

file := filesystem workingDirectory / 'Chapters' / 'SUnit'
/ 'anExample1.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'anExample1.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
header: [ builder text: 'Foo' ] withLevel: 1;
text: 'Pharo is cool';
codeblock: 'This is a code' ].

file := filesystem workingDirectory / 'Chapters' / 'SUnit'
/ 'SUnit.md'.
file := filesystem workingDirectory / 'Chapters' / 'SUnit' / 'SUnit.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
inputfile: 'anExample1.md' ].

reference := (MicResourceReference fromUri:
'file:///Chapters/SUnit/SUnit.md') fileSystem:
filesystem.
reference := (MicResourceReference fromUri: 'file:///Chapters/SUnit/SUnit.md') fileSystem: filesystem.
root := reference loadMicrodown.

self assert: root children first class equals: MicInputfileBlock.
self assert:
(root children first path isKindOf: MicResourceReference).
self assert: (root children first path isKindOf: MicResourceReference).

includer visit: root.

self assert: root children first class equals: MicHeaderBlock.
self
assert: root children first children first bodyString
equals: 'Foo'.
self assert: root children second class equals: MicParagraphBlock.
self assert: root children third class equals: MicCodeBlock
self assert: root children first class equals: MicSecondaryRootBlock.
self assert: root children first children first class equals: MicHeaderBlock.
self assert: root children first children first children first bodyString equals: 'Foo'.
self assert: root children first children second class equals: MicParagraphBlock.
self assert: root children first children third class equals: MicCodeBlock
]

{ #category : 'tests' }
Expand All @@ -227,26 +207,23 @@ MicFileIncluderTest >> testIncluderIncludesInputFilesWithinTheSameParagraph [
setStream: stream;
inputfile: 'anExample1.md';
inputfile: 'anExample2.md' ].
reference := (MicResourceReference fromUri: 'file:///test.md')
fileSystem: filesystem.
reference := (MicResourceReference fromUri: 'file:///test.md') fileSystem: filesystem.
root := reference loadMicrodown.
includer visit: root.

self assert: root children first class equals: MicHeaderBlock.
self
assert: root children first children first bodyString
equals: 'Foo'.
self assert: root children first level equals: 1.
self assert: root children second class equals: MicParagraphBlock.
self assert: root children third class equals: MicCodeBlock.

self assert: root children fourth class equals: MicHeaderBlock.
self
assert: root children fourth children first bodyString
equals: 'Bar'.
self assert: root children fourth level equals: 2.
self assert: root children fifth class equals: MicParagraphBlock.
self assert: root children sixth class equals: MicCodeBlock
self assert: root children first class equals: MicSecondaryRootBlock.
self assert: root children first children first class equals: MicHeaderBlock.
self assert: root children first children first children first bodyString equals: 'Foo'.
self assert: root children first children first level equals: 1.
self assert: root children first children second class equals: MicParagraphBlock.
self assert: root children first children third class equals: MicCodeBlock.

self assert: root children second class equals: MicSecondaryRootBlock.
self assert: root children second children first class equals: MicHeaderBlock.
self assert: root children second children first children first bodyString equals: 'Bar'.
self assert: root children second children first level equals: 2.
self assert: root children second children second class equals: MicParagraphBlock.
self assert: root children second children third class equals: MicCodeBlock
]

{ #category : 'tests' }
Expand All @@ -270,26 +247,26 @@ MicFileIncluderTest >> testIncluderJustReturnInputFileBlockWhenIncludedFileDoesN

{ #category : 'tests' }
MicFileIncluderTest >> testMicFileInclusionTransformMicInputFiles [

| root file reference |
file := filesystem workingDirectory / 'test.md'.
file writeStreamDo: [ :stream |
builder
setStream: stream;
setStream: stream;
inputfile: 'anExample1.md';
paragraph: '';
inputfile: 'anExample2.md' ].
reference := (MicResourceReference fromUri: 'file:///test.md') fileSystem: filesystem.
root := reference loadMicrodown.
includer visit: root.

self assert: root children first class equals: MicHeaderBlock.
self assert: root children second class equals: MicParagraphBlock.
self assert: root children third class equals: MicCodeBlock.

self assert: root children fourth class equals: MicHeaderBlock.
self assert: root children fifth class equals: MicParagraphBlock.
self assert: root children first class equals: MicSecondaryRootBlock.
self assert: root children first children first class equals: MicHeaderBlock.
self assert: root children first children second class equals: MicParagraphBlock.
self assert: root children first children third class equals: MicCodeBlock.

self assert: root children fifth children size equals: 2.
self assert: root children sixth class equals: MicCodeBlock
self assert: root children second class equals: MicSecondaryRootBlock.
self assert: root children second children first class equals: MicHeaderBlock.
self assert: root children second children second class equals: MicParagraphBlock.
self assert: root children second children second children size equals: 2.
self assert: root children second children third class equals: MicCodeBlock
]
8 changes: 6 additions & 2 deletions src/Microdown-Transformer/MicFileIncluder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ MicFileIncluder >> topFile: aFileReference [
MicFileIncluder >> visitInputFile: anInputFileAnnotation [
"I load the file and if the file exist I replace the node of the annotation by the content of the file."

| inputRef inputDoc |
| inputRef inputDoc newRoot |
inputRef := anInputFileAnnotation path.
[ inputDoc := inputRef loadMicrodown inlineInputFiles]
on: MicResourceReferenceError
do: [ :error | isStrict
ifFalse: [ ^ self ]
ifTrue: [ error pass ]].

self replaceCurrentNodeBy: inputDoc children
newRoot := MicSecondaryRootBlock new.
newRoot fromFile: inputRef.
newRoot children: inputDoc children.
newRoot children do: [ :each | each parent: newRoot ].

self replaceCurrentNodeBy: { newRoot }
]
5 changes: 5 additions & 0 deletions src/Microdown/MicRootBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ MicRootBlock >> initialize [
debugId := self class nextDebugId.
]

{ #category : 'debugging' }
MicRootBlock >> isSecondaryRoot [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be smarter. I wonder. Because a main root block parent is nil while by construction a secondaryRootBlock should be nested into something.

^ false
]

{ #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."
Expand Down
23 changes: 23 additions & 0 deletions src/Microdown/MicSecondaryRootBlock.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Class {
#name : 'MicSecondaryRootBlock',
#superclass : 'MicRootBlock',
#category : 'Microdown-Model',
#package : 'Microdown',
#tag : 'Model'
}

{ #category : 'testing' }
MicSecondaryRootBlock >> accept: aVisitor [

^ aVisitor visitSecondaryRootBlock: self
]

{ #category : 'testing' }
MicSecondaryRootBlock >> fromFile: aFileOrResource [
self propertyAt: #file put: aFileOrResource

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense que nous avons la meme methode dans la superclasse

]

{ #category : 'testing' }
MicSecondaryRootBlock >> isSecondaryRoot [
^ true
]
6 changes: 6 additions & 0 deletions src/Microdown/MicrodownVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ MicrodownVisitor >> visitScript: aSlide [
^ self visitChildrenOf: aSlide
]

{ #category : 'visiting' }
MicrodownVisitor >> visitSecondaryRootBlock: aSecondaryRootBlock [

^ self visitRoot: aSecondaryRootBlock
]

{ #category : 'visiting' }
MicrodownVisitor >> visitSegment: aSegment [
"subclassResponsibility"
Expand Down
Loading