Improving Markup File and Markdown metadata extraction.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2020-07-03 18:51:45 -05:00
parent 6850cdd53a
commit 51f09e40bd
2 changed files with 33 additions and 10 deletions

View File

@ -18,6 +18,12 @@ Markdown class >> fromFile: aFileReference [
^ self new fromFile: aFileReference
]
{ #category : #utilities }
Markdown class >> yamlMetadataDelimiter [
^ '---'
]
{ #category : #operation }
Markdown >> commentYAMLMetadata [
| newContents |
@ -34,7 +40,7 @@ Markdown >> commentYAMLMetadata [
^ self contents
]
{ #category : #utility }
{ #category : #utilities }
Markdown >> containsYAMLMetadataClosing [
^ self locateYAMLMetadataClosing > 0
]
@ -49,13 +55,13 @@ Markdown >> contents: anObject [
contents := anObject
]
{ #category : #utility }
{ #category : #utilities }
Markdown >> detectYAMLMetadata [
| lines |
lines := self lines.
^ self startsWithYAMLMetadataDelimiter
and: [ lines allButFirst
detect: [ :currentLine | currentLine beginsWith: '---' ]
detect: [ :currentLine | currentLine beginsWith: self class yamlMetadataDelimiter ]
ifFound: [ ^ true ] ifNone: [ ^ false ] ]
]
@ -70,24 +76,38 @@ Markdown >> fromFile: aFileReference [
self contents: aFileReference contents.
]
{ #category : #utility }
{ #category : #utilities }
Markdown >> lines [
^ self contents lines.
]
{ #category : #utility }
{ #category : #utilities }
Markdown >> locateYAMLMetadataClosing [
"I return the line where the closing of the YAML metadata occurs or 0 if no closing is found."
| result |
self startsWithYAMLMetadataDelimiter ifFalse: [ ^ self ].
result := 0.
self lines allButFirst doWithIndex: [ :currentLine :i |
(currentLine beginsWith: '---') ifTrue: [ result := i ] ].
(currentLine beginsWith: self class yamlMetadataDelimiter) ifTrue: [ result := i ]].
^ result
]
{ #category : #utility }
{ #category : #utilities }
Markdown >> startsWithYAMLMetadataDelimiter [
^ self lines first beginsWith: '---'
^ self lines first beginsWith: self class yamlMetadataDelimiter
]
{ #category : #utilities }
Markdown >> yamlMetadataAsString [
| output |
self extractYAMLMetadata ifNil: [ ^ nil ].
output := String new writeStream.
output nextPutAll: self class yamlMetadataDelimiter; cr.
self extractYAMLMetadata do: [ :line |
output nextPutAll: line; cr.
].
output nextPutAll: self class yamlMetadataDelimiter; cr.
^ output contents.
]

View File

@ -1,3 +1,6 @@
"
I model common operations made with several markup files.
"
Class {
#name : #MarkupFile,
#superclass : #Object,
@ -8,11 +11,11 @@ Class {
}
{ #category : #persistence }
MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: contents [
MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: text [
| file |
file := aFileReferenceOrFileName asFileReference.
file exists ifFalse: [ file ensureCreateFile ].
file writeStreamDo: [ :stream |
stream nextPutAll: contents ].
stream nextPutAll: text withInternetLineEndings].
self inform: 'Exported as: ', String cr, file fullName
]