diff --git a/src/MiniDocs/LePage.extension.st b/src/MiniDocs/LePage.extension.st index 7426bae..ba36168 100644 --- a/src/MiniDocs/LePage.extension.st +++ b/src/MiniDocs/LePage.extension.st @@ -49,17 +49,12 @@ LePage >> asMarkdown [ | bodyStream markdown | bodyStream := '' writeStream. bodyStream - nextPutAll: '---'; - nextPutAll: String lf. - self metadata keysAndValuesDo: [ :k :v | - bodyStream - nextPutAll: k , ': "' , v, '"'; - nextPutAll: String lf ]. - bodyStream nextPutAll: '---' , String lf , String lf, String lf. - bodyStream nextPutAll: '# ', self title, String lf , String lf. + nextPutAll: '# ', self title; cr; cr. self preorderTraversal do: [ :snippet | bodyStream nextPutAll: snippet asMarkdown ]. - markdown := Markdown new contents: bodyStream contents. + markdown := Markdown new + contents: bodyStream contents demoteMarkdownHeaders; + metadata: (self metadata at: 'original' ifAbsentPut: Dictionary new). ^ markdown ] @@ -67,7 +62,21 @@ LePage >> asMarkdown [ LePage >> asMarkdownFile [ | folder | folder := self storage. - ^ MarkupFile exportAsFileOn: folder / self markdownFileName containing: self asMarkdown contents + ^ MarkupFile exportAsFileOn: folder / self markdownFileName containing: self asMarkdownWithMetadataWrappers contents +] + +{ #category : #'*MiniDocs' } +LePage >> asMarkdownWithMetadataWrappers [ + | bodyStream markdown | + bodyStream := '' writeStream. + bodyStream + nextPutAll: '# ', self title; cr; cr. + self preorderTraversal + do: [ :snippet | bodyStream nextPutAll: snippet asMarkdownWithMetadataWrappers ]. + markdown := Markdown new + contents: bodyStream contents demoteMarkdownHeaders; + metadata: (self metadata at: 'original' ifAbsentPut: Dictionary new). + ^ markdown ] { #category : #'*MiniDocs' } diff --git a/src/MiniDocs/LePictureSnippet.extension.st b/src/MiniDocs/LePictureSnippet.extension.st index 77df79c..12dedb0 100644 --- a/src/MiniDocs/LePictureSnippet.extension.st +++ b/src/MiniDocs/LePictureSnippet.extension.st @@ -26,7 +26,7 @@ LePictureSnippet >> asMarkdeep [ ] { #category : #'*MiniDocs' } -LePictureSnippet >> asMarkdown [ +LePictureSnippet >> asMarkdownWithMetadataWrappers [ ^ self asMarkdeep ] diff --git a/src/MiniDocs/LeTextualSnippet.extension.st b/src/MiniDocs/LeTextualSnippet.extension.st index ee61c3a..dc850f4 100644 --- a/src/MiniDocs/LeTextualSnippet.extension.st +++ b/src/MiniDocs/LeTextualSnippet.extension.st @@ -24,6 +24,16 @@ LeTextualSnippet >> asMarkdeep [ { #category : #'*MiniDocs' } LeTextualSnippet >> asMarkdown [ + + | output | + output := '' writeStream. + output + nextPutAll: self contentAsStringCustomized; lf. + ^ output contents +] + +{ #category : #'*MiniDocs' } +LeTextualSnippet >> asMarkdownWithMetadataWrappers [ "Inspired by Alpine.js and Assembler CSS 'x-' properties, we are going to use 'st-' properties as a way to extend divs metadata regarding its contents." | output | diff --git a/src/MiniDocs/Markdeep.class.st b/src/MiniDocs/Markdeep.class.st index 8ccb46e..bac2dfc 100644 --- a/src/MiniDocs/Markdeep.class.st +++ b/src/MiniDocs/Markdeep.class.st @@ -30,7 +30,7 @@ Markdeep class >> fromPubPubTOC: orderedDictionary folder: folder index: ordina ] { #category : #accessing } -Markdeep >> asMarkdown [ +Markdeep >> asMarkdownWithMetadataWrappers [ ^ Markdown new metadata: self metadata; body: self body; diff --git a/src/MiniDocs/Markdown.class.st b/src/MiniDocs/Markdown.class.st index 5bbfd1e..4964521 100644 --- a/src/MiniDocs/Markdown.class.st +++ b/src/MiniDocs/Markdown.class.st @@ -149,7 +149,7 @@ Markdown >> fromFile: aFileReference [ { #category : #'instance creation' } Markdown >> fromString: markdownString [ - self metadata: markdownString yamlMetadata. + (self metadata) at: 'original' put: markdownString yamlMetadata. self body: markdownString contentsWithoutYAMLMetadata ] diff --git a/src/MiniDocs/String.extension.st b/src/MiniDocs/String.extension.st index 1b775c0..3c7f8ac 100644 --- a/src/MiniDocs/String.extension.st +++ b/src/MiniDocs/String.extension.st @@ -51,6 +51,15 @@ String >> deleteYAMLMetadata [ ^ newContents contents. ] +{ #category : #'*MiniDocs' } +String >> demoteMarkdownHeaders [ + | response | + response := self contents lines. + self markdownHeaders associations allButFirstDo: [ :assoc | + response at: assoc key put: '#', assoc value ]. + ^ response asStringWithCr withInternetLineEndings +] + { #category : #'*MiniDocs' } String >> detectYAMLMetadata [ | lines | @@ -62,15 +71,26 @@ String >> detectYAMLMetadata [ ] { #category : #'*MiniDocs' } -String >> promoteMarkdownHeaders [ - | headers response | - response := self contents. - headers := (LeTextSnippet string: response) ast // #LeHeaderNode collect: [ :each | each headerFullName asString ]. - headers do: [ :each | - response := response copyReplaceAll: each with: each allButFirst ]. +String >> markdownHeaders [ + | response headers | + headers := (LeTextSnippet string: self contents) ast // #LeHeaderNode collect: [ :each | each headerFullName asString ]. + response := OrderedDictionary new. + self lines doWithIndex: [:line :index | + (line beginsWithAnyOf: headers) + ifTrue: [ response at: index put: line ] + ]. ^ response ] +{ #category : #'*MiniDocs' } +String >> promoteMarkdownHeaders [ + | response | + response := self contents lines. + self markdownHeaders associationsDo: [ :assoc | + response at: assoc key put: assoc value allButFirst ]. + ^ response asStringWithCr withInternetLineEndings +] + { #category : #'*MiniDocs' } String >> romanizeAccents [ | modified corrections |