Moving basic YAML metadata capabilities to the String class.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2024-03-25 11:16:53 -05:00
parent b7fdbb731c
commit 0185c741a1
3 changed files with 82 additions and 79 deletions

View File

@ -20,12 +20,6 @@ Markdown class >> fromFile: aFileReference [
^ self new fromFile: aFileReference ^ self new fromFile: aFileReference
] ]
{ #category : #utilities }
Markdown class >> yamlMetadataDelimiter [
^ '---'
]
{ #category : #accessing } { #category : #accessing }
Markdown >> asMarkdeep [ Markdown >> asMarkdeep [
^ Markdeep new ^ Markdeep new
@ -74,8 +68,8 @@ Markdown >> contents [
] ]
{ #category : #accessing } { #category : #accessing }
Markdown >> contents: anObject [ Markdown >> contents: aString [
body := anObject body := aString
] ]
{ #category : #accessing } { #category : #accessing }
@ -88,26 +82,6 @@ Markdown >> contentsWithoutYAMLMetadata [
^ newContents contents. ^ newContents contents.
] ]
{ #category : #operation }
Markdown >> deleteYAMLMetadata [
| newContents |
self detectYAMLMetadata ifFalse: [ ^ self ].
newContents := '' writeStream.
(self lines copyFrom: self yamlMetadataClosingLineNumber + 1 to: self lines size) do: [ :line |
newContents nextPutAll: line; lf;lf ].
^ newContents contents.
]
{ #category : #utilities }
Markdown >> detectYAMLMetadata [
| lines |
lines := self lines.
^ self startsWithYAMLMetadataDelimiter
and: [ lines allButFirst
detect: [ :currentLine | currentLine beginsWith: self class yamlMetadataDelimiter ]
ifFound: [ ^ true ] ifNone: [ ^ false ] ]
]
{ #category : #accessing } { #category : #accessing }
Markdown >> documentTree [ Markdown >> documentTree [
| parser| | parser|
@ -181,14 +155,14 @@ Markdown >> file: aFileReference [
Markdown >> fromFile: aFileReference [ Markdown >> fromFile: aFileReference [
self contents: aFileReference contents. self contents: aFileReference contents.
self file: aFileReference. self file: aFileReference.
self populateMetadata. self yamlMetadata.
self body: self contentsWithoutYAMLMetadata self body: self contentsWithoutYAMLMetadata
] ]
{ #category : #'instance creation' } { #category : #'instance creation' }
Markdown >> fromString: markdownString [ Markdown >> fromString: markdownString [
self contents: markdownString. self contents: markdownString.
self populateMetadata. self yamlMetadata.
self contents: self contentsWithoutYAMLMetadata self contents: self contentsWithoutYAMLMetadata
] ]
@ -245,11 +219,6 @@ Markdown >> options [
^ self metadata at: 'options' ifAbsentPut: [ self defaultOptions] ^ self metadata at: 'options' ifAbsentPut: [ self defaultOptions]
] ]
{ #category : #accessing }
Markdown >> populateMetadata [
self metadata: (YAML2JSON fromString: self yamlMetadataString)
]
{ #category : #accessing } { #category : #accessing }
Markdown >> printOn: aStream [ Markdown >> printOn: aStream [
super printOn: aStream. super printOn: aStream.
@ -257,48 +226,7 @@ Markdown >> printOn: aStream [
nextPutAll: '( ', self title , ' )' nextPutAll: '( ', self title , ' )'
] ]
{ #category : #utilities }
Markdown >> startsWithYAMLMetadataDelimiter [
self lines ifEmpty: [^false].
^ self lines first beginsWith: self class yamlMetadataDelimiter
]
{ #category : #accessing } { #category : #accessing }
Markdown >> title [ Markdown >> title [
^ self metadata at: 'title' ifAbsentPut: [ self headerAsTitle] ^ self metadata at: 'title' ifAbsentPut: [ self headerAsTitle]
] ]
{ #category : #utilities }
Markdown >> yamlMetadataClosingLineNumber [
"I return the line where the closing of the YAML metadata occurs or 0 if no closing is found."
self startsWithYAMLMetadataDelimiter ifFalse: [ ^ self ].
self lines allButFirst doWithIndex: [ :currentLine :i |
(currentLine beginsWith: self class yamlMetadataDelimiter) ifTrue: [ ^ i + 1 ]]
]
{ #category : #operation }
Markdown >> yamlMetadataString [
| output yamlLines |
self detectYAMLMetadata ifFalse: [ ^nil ].
self lines ifEmpty: [ ^nil ].
yamlLines := self lines copyFrom: 2 to: self yamlMetadataClosingLineNumber - 1.
output := '' writeStream.
yamlLines do: [ :line |
output
nextPutAll: line;
nextPut: Character lf. ].
^ output contents
]
{ #category : #utilities }
Markdown >> yamlMetadataStringWithDelimiters [
| output |
self yamlMetadataString ifNil: [ ^ nil ].
output := String new writeStream.
output nextPutAll: self class yamlMetadataDelimiter; cr.
output nextPutAll: self yamlMetadataString.
output nextPutAll: self class yamlMetadataDelimiter; cr.
^ output contents.
]

View File

@ -11,13 +11,16 @@ Class {
} }
{ #category : #persistence } { #category : #persistence }
MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: aString [ MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: anObject [
| file | | file preprocessed |
file := aFileReferenceOrFileName asFileReference. file := aFileReferenceOrFileName asFileReference.
file ensureDelete. file ensureDelete.
file exists ifFalse: [ file ensureCreateFile ]. file exists ifFalse: [ file ensureCreateFile ].
anObject className = String
ifTrue: [ preprocessed := anObject withInternetLineEndings ]
ifFalse: [preprocessed := STON toStringPretty: anObject ].
file writeStreamDo: [ :stream | file writeStreamDo: [ :stream |
stream nextPutAll: ( aString ) withInternetLineEndings]. stream nextPutAll: preprocessed ].
self inform: 'Exported as: ', String cr, file fullName. self inform: 'Exported as: ', String cr, file fullName.
^ file ^ file
] ]

View File

@ -20,6 +20,26 @@ String >> asDashedLowercase [
^ '-' join: (self substrings collect: [:each | each asLowercase ]) ^ '-' join: (self substrings collect: [:each | each asLowercase ])
] ]
{ #category : #'*MiniDocs' }
String >> deleteYAMLMetadata [
| newContents |
self detectYAMLMetadata ifFalse: [ ^ self ].
newContents := '' writeStream.
(self lines copyFrom: self yamlMetadataClosingLineNumber + 1 to: self lines size) do: [ :line |
newContents nextPutAll: line; lf;lf ].
^ newContents contents.
]
{ #category : #'*MiniDocs' }
String >> detectYAMLMetadata [
| lines |
lines := self lines.
^ self startsWithYAMLMetadataDelimiter
and: [ lines allButFirst
detect: [ :currentLine | currentLine beginsWith: self class yamlMetadataDelimiter ]
ifFound: [ ^ true ] ifNone: [ ^ false ] ]
]
{ #category : #'*MiniDocs' } { #category : #'*MiniDocs' }
String >> romanizeAccents [ String >> romanizeAccents [
| modified corrections | | modified corrections |
@ -33,7 +53,59 @@ String >> romanizeAccents [
^ modified ^ modified
] ]
{ #category : #'*MiniDocs' }
String >> startsWithYAMLMetadataDelimiter [
self lines ifEmpty: [^false].
^ self lines first beginsWith: self class yamlMetadataDelimiter
]
{ #category : #'*MiniDocs' } { #category : #'*MiniDocs' }
String >> withoutXMLTagDelimiters [ String >> withoutXMLTagDelimiters [
^ self copyWithoutAll: #($< $>) ^ self copyWithoutAll: #($< $>)
] ]
{ #category : #'*MiniDocs' }
String >> yamlMetadata [
^ (YAML2JSON fromString: self yamlMetadataString)
]
{ #category : #'*MiniDocs' }
String >> yamlMetadataClosingLineNumber [
"I return the line where the closing of the YAML metadata occurs or 0 if no closing is found."
self startsWithYAMLMetadataDelimiter ifFalse: [ ^ self ].
self lines allButFirst doWithIndex: [ :currentLine :i |
(currentLine beginsWith: self class yamlMetadataDelimiter) ifTrue: [ ^ i + 1 ]]
]
{ #category : #'*MiniDocs' }
String class >> yamlMetadataDelimiter [
^ '---'
]
{ #category : #'*MiniDocs' }
String >> yamlMetadataString [
| output yamlLines |
self detectYAMLMetadata ifFalse: [ ^nil ].
self lines ifEmpty: [ ^nil ].
yamlLines := self lines copyFrom: 2 to: self yamlMetadataClosingLineNumber - 1.
output := '' writeStream.
yamlLines do: [ :line |
output
nextPutAll: line;
nextPut: Character lf. ].
^ output contents
]
{ #category : #'*MiniDocs' }
String >> yamlMetadataStringWithDelimiters [
| output |
self yamlMetadataString ifNil: [ ^ nil ].
output := String new writeStream.
output nextPutAll: self class yamlMetadataDelimiter; cr.
output nextPutAll: self yamlMetadataString.
output nextPutAll: self class yamlMetadataDelimiter; cr.
^ output contents.
]