From 2a458d2393f4e6739b71b6b361d6ebfa1908bd12 Mon Sep 17 00:00:00 2001 From: OffrayLuna Date: Tue, 18 Jun 2019 11:26:52 +0000 Subject: [PATCH] Adding Markdown with YAML metadata detection. --- src/Grafoscopio-Utils/Markdown.class.st | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Grafoscopio-Utils/Markdown.class.st diff --git a/src/Grafoscopio-Utils/Markdown.class.st b/src/Grafoscopio-Utils/Markdown.class.st new file mode 100644 index 0000000..76bd49c --- /dev/null +++ b/src/Grafoscopio-Utils/Markdown.class.st @@ -0,0 +1,77 @@ +" +I model a Markdown document. +At some point the idea is to have a full native parser implemented to deal +with my syntax, but meanwhile I will be collaborating with external parsers, +particularly the ones provided by Pandoc and/or Lunamark. +" +Class { + #name : #Markdown, + #superclass : #Object, + #instVars : [ + 'contents' + ], + #category : #'Grafoscopio-Utils' +} + +{ #category : #'instance creation' } +Markdown class >> fromFile: aFileReference [ + ^ self new fromFile: aFileReference +] + +{ #category : #utility } +Markdown >> containsYAMLMetadataClosing [ + ^ self locateYAMLMetadataClosing > 0 +] + +{ #category : #accessing } +Markdown >> contents [ + ^ contents +] + +{ #category : #accessing } +Markdown >> contents: anObject [ + contents := anObject +] + +{ #category : #utility } +Markdown >> detectYAMLMetadata [ + | lines | + lines := self lines. + ^ self startsWithYAMLMetadataDelimiter + and: [ lines allButFirst + detect: [ :currentLine | currentLine beginsWith: '---' ] + ifFound: [ ^ true ] ifNone: [ ^ false ] ] +] + +{ #category : #operation } +Markdown >> extractYAMLMetadata [ + self detectYAMLMetadata ifFalse: [ ^ nil ]. + ^ self lines copyFrom: 2 to: (self locateYAMLMetadataClosing) +] + +{ #category : #'instance creation' } +Markdown >> fromFile: aFileReference [ + self contents: aFileReference contents. +] + +{ #category : #utility } +Markdown >> lines [ + ^ self contents lines. +] + +{ #category : #utility } +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 ] ]. + ^ result +] + +{ #category : #utility } +Markdown >> startsWithYAMLMetadataDelimiter [ + ^ self lines first beginsWith: '---' + +]