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 ^ self new fromFile: aFileReference
] ]
{ #category : #utilities }
Markdown class >> yamlMetadataDelimiter [
^ '---'
]
{ #category : #operation } { #category : #operation }
Markdown >> commentYAMLMetadata [ Markdown >> commentYAMLMetadata [
| newContents | | newContents |
@ -34,7 +40,7 @@ Markdown >> commentYAMLMetadata [
^ self contents ^ self contents
] ]
{ #category : #utility } { #category : #utilities }
Markdown >> containsYAMLMetadataClosing [ Markdown >> containsYAMLMetadataClosing [
^ self locateYAMLMetadataClosing > 0 ^ self locateYAMLMetadataClosing > 0
] ]
@ -49,13 +55,13 @@ Markdown >> contents: anObject [
contents := anObject contents := anObject
] ]
{ #category : #utility } { #category : #utilities }
Markdown >> detectYAMLMetadata [ Markdown >> detectYAMLMetadata [
| lines | | lines |
lines := self lines. lines := self lines.
^ self startsWithYAMLMetadataDelimiter ^ self startsWithYAMLMetadataDelimiter
and: [ lines allButFirst and: [ lines allButFirst
detect: [ :currentLine | currentLine beginsWith: '---' ] detect: [ :currentLine | currentLine beginsWith: self class yamlMetadataDelimiter ]
ifFound: [ ^ true ] ifNone: [ ^ false ] ] ifFound: [ ^ true ] ifNone: [ ^ false ] ]
] ]
@ -70,24 +76,38 @@ Markdown >> fromFile: aFileReference [
self contents: aFileReference contents. self contents: aFileReference contents.
] ]
{ #category : #utility } { #category : #utilities }
Markdown >> lines [ Markdown >> lines [
^ self contents lines. ^ self contents lines.
] ]
{ #category : #utility } { #category : #utilities }
Markdown >> locateYAMLMetadataClosing [ Markdown >> locateYAMLMetadataClosing [
"I return the line where the closing of the YAML metadata occurs or 0 if no closing is found." "I return the line where the closing of the YAML metadata occurs or 0 if no closing is found."
| result | | result |
self startsWithYAMLMetadataDelimiter ifFalse: [ ^ self ]. self startsWithYAMLMetadataDelimiter ifFalse: [ ^ self ].
result := 0. result := 0.
self lines allButFirst doWithIndex: [ :currentLine :i | self lines allButFirst doWithIndex: [ :currentLine :i |
(currentLine beginsWith: '---') ifTrue: [ result := i ] ]. (currentLine beginsWith: self class yamlMetadataDelimiter) ifTrue: [ result := i ]].
^ result ^ result
] ]
{ #category : #utility } { #category : #utilities }
Markdown >> startsWithYAMLMetadataDelimiter [ 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 { Class {
#name : #MarkupFile, #name : #MarkupFile,
#superclass : #Object, #superclass : #Object,
@ -8,11 +11,11 @@ Class {
} }
{ #category : #persistence } { #category : #persistence }
MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: contents [ MarkupFile class >> exportAsFileOn: aFileReferenceOrFileName containing: text [
| file | | file |
file := aFileReferenceOrFileName asFileReference. file := aFileReferenceOrFileName asFileReference.
file exists ifFalse: [ file ensureCreateFile ]. file exists ifFalse: [ file ensureCreateFile ].
file writeStreamDo: [ :stream | file writeStreamDo: [ :stream |
stream nextPutAll: contents ]. stream nextPutAll: text withInternetLineEndings].
self inform: 'Exported as: ', String cr, file fullName self inform: 'Exported as: ', String cr, file fullName
] ]