Markdown can be exported with or without metadata wrappers.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2024-04-09 16:03:47 -05:00
parent 8fb6373a83
commit 51e84c2404
6 changed files with 58 additions and 19 deletions

View File

@ -49,17 +49,12 @@ LePage >> asMarkdown [
| bodyStream markdown | | bodyStream markdown |
bodyStream := '' writeStream. bodyStream := '' writeStream.
bodyStream bodyStream
nextPutAll: '---'; nextPutAll: '# ', self title; cr; cr.
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.
self preorderTraversal self preorderTraversal
do: [ :snippet | bodyStream nextPutAll: snippet asMarkdown ]. 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 ^ markdown
] ]
@ -67,7 +62,21 @@ LePage >> asMarkdown [
LePage >> asMarkdownFile [ LePage >> asMarkdownFile [
| folder | | folder |
folder := self storage. 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' } { #category : #'*MiniDocs' }

View File

@ -26,7 +26,7 @@ LePictureSnippet >> asMarkdeep [
] ]
{ #category : #'*MiniDocs' } { #category : #'*MiniDocs' }
LePictureSnippet >> asMarkdown [ LePictureSnippet >> asMarkdownWithMetadataWrappers [
^ self asMarkdeep ^ self asMarkdeep
] ]

View File

@ -24,6 +24,16 @@ LeTextualSnippet >> asMarkdeep [
{ #category : #'*MiniDocs' } { #category : #'*MiniDocs' }
LeTextualSnippet >> asMarkdown [ 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 "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." 'st-' properties as a way to extend divs metadata regarding its contents."
| output | | output |

View File

@ -30,7 +30,7 @@ Markdeep class >> fromPubPubTOC: orderedDictionary folder: folder index: ordina
] ]
{ #category : #accessing } { #category : #accessing }
Markdeep >> asMarkdown [ Markdeep >> asMarkdownWithMetadataWrappers [
^ Markdown new ^ Markdown new
metadata: self metadata; metadata: self metadata;
body: self body; body: self body;

View File

@ -149,7 +149,7 @@ Markdown >> fromFile: aFileReference [
{ #category : #'instance creation' } { #category : #'instance creation' }
Markdown >> fromString: markdownString [ Markdown >> fromString: markdownString [
self metadata: markdownString yamlMetadata. (self metadata) at: 'original' put: markdownString yamlMetadata.
self body: markdownString contentsWithoutYAMLMetadata self body: markdownString contentsWithoutYAMLMetadata
] ]

View File

@ -51,6 +51,15 @@ String >> deleteYAMLMetadata [
^ newContents contents. ^ 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' } { #category : #'*MiniDocs' }
String >> detectYAMLMetadata [ String >> detectYAMLMetadata [
| lines | | lines |
@ -62,15 +71,26 @@ String >> detectYAMLMetadata [
] ]
{ #category : #'*MiniDocs' } { #category : #'*MiniDocs' }
String >> promoteMarkdownHeaders [ String >> markdownHeaders [
| headers response | | response headers |
response := self contents. headers := (LeTextSnippet string: self contents) ast // #LeHeaderNode collect: [ :each | each headerFullName asString ].
headers := (LeTextSnippet string: response) ast // #LeHeaderNode collect: [ :each | each headerFullName asString ]. response := OrderedDictionary new.
headers do: [ :each | self lines doWithIndex: [:line :index |
response := response copyReplaceAll: each with: each allButFirst ]. (line beginsWithAnyOf: headers)
ifTrue: [ response at: index put: line ]
].
^ response ^ 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' } { #category : #'*MiniDocs' }
String >> romanizeAccents [ String >> romanizeAccents [
| modified corrections | | modified corrections |