2019-06-18 11:26:52 +00:00
|
|
|
"
|
|
|
|
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 : [
|
2020-07-10 22:52:16 +00:00
|
|
|
'contents',
|
|
|
|
'file'
|
2019-06-18 11:26:52 +00:00
|
|
|
],
|
|
|
|
#category : #'Grafoscopio-Utils'
|
|
|
|
}
|
|
|
|
|
|
|
|
{ #category : #'instance creation' }
|
|
|
|
Markdown class >> fromFile: aFileReference [
|
|
|
|
^ self new fromFile: aFileReference
|
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
|
|
|
Markdown class >> yamlMetadataDelimiter [
|
|
|
|
^ '---'
|
|
|
|
|
|
|
|
]
|
|
|
|
|
2019-07-05 12:19:10 +00:00
|
|
|
{ #category : #operation }
|
|
|
|
Markdown >> commentYAMLMetadata [
|
|
|
|
| newContents |
|
|
|
|
self detectYAMLMetadata ifFalse: [ ^ self ].
|
|
|
|
newContents := '' writeStream.
|
|
|
|
newContents nextPutAll: '<!--@yaml:'; crlf.
|
|
|
|
self extractYAMLMetadata do: [ :line |
|
|
|
|
newContents nextPutAll: line ].
|
|
|
|
newContents nextPutAll: String cr.
|
|
|
|
newContents nextPutAll: '-->'; crlf.
|
|
|
|
(self lines copyFrom: self locateYAMLMetadataClosing + 2 to: self lines size) do: [ :line |
|
|
|
|
newContents nextPutAll: line; crlf ].
|
|
|
|
self contents: newContents contents.
|
|
|
|
^ self contents
|
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
2019-06-18 11:26:52 +00:00
|
|
|
Markdown >> containsYAMLMetadataClosing [
|
|
|
|
^ self locateYAMLMetadataClosing > 0
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
Markdown >> contents [
|
|
|
|
^ contents
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
Markdown >> contents: anObject [
|
|
|
|
contents := anObject
|
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
2019-06-18 11:26:52 +00:00
|
|
|
Markdown >> detectYAMLMetadata [
|
|
|
|
| lines |
|
|
|
|
lines := self lines.
|
|
|
|
^ self startsWithYAMLMetadataDelimiter
|
|
|
|
and: [ lines allButFirst
|
2020-07-03 23:51:45 +00:00
|
|
|
detect: [ :currentLine | currentLine beginsWith: self class yamlMetadataDelimiter ]
|
2019-06-18 11:26:52 +00:00
|
|
|
ifFound: [ ^ true ] ifNone: [ ^ false ] ]
|
|
|
|
]
|
|
|
|
|
2020-07-10 22:52:16 +00:00
|
|
|
{ #category : #operation }
|
|
|
|
Markdown >> exportMetadataAsJson [
|
2020-10-08 22:36:52 +00:00
|
|
|
"TBD: Lua scripts should be checked and installed when missing. Maybe a shared location
|
|
|
|
in '.local/share/Grafoscopio/Scripts' should be developed in the near future."
|
|
|
|
| output luaScript |
|
|
|
|
luaScript := FileLocator home / '.local/share/Brea/scripts/meta-to-json.lua'.
|
2020-10-09 18:22:59 +00:00
|
|
|
Smalltalk platformName = 'unix' ifTrue: [
|
|
|
|
OSSUnixSubprocess new
|
|
|
|
workingDirectory: self file parent fullName;
|
|
|
|
command: 'pandoc';
|
|
|
|
arguments: { '--lua-filter=', luaScript fullName . self file basename };
|
|
|
|
redirectStdout;
|
|
|
|
redirectStdin;
|
|
|
|
runAndWaitOnExitDo: [ :process :outString :errString |
|
|
|
|
output := process isSuccess
|
|
|
|
ifTrue: [ outString ]
|
|
|
|
ifFalse: [ errString ]
|
|
|
|
]].
|
2020-10-08 22:36:52 +00:00
|
|
|
^ output correctAccentedCharacters
|
2020-07-10 22:52:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #operation }
|
|
|
|
Markdown >> exportMetadataAsYaml [
|
|
|
|
| exportedFile |
|
|
|
|
exportedFile := FileLocator temp / 'metadata.yaml'.
|
|
|
|
MarkupFile exportAsFileOn: exportedFile containing: self yamlMetadataAsString.
|
|
|
|
^ exportedFile
|
|
|
|
]
|
|
|
|
|
2019-06-18 11:26:52 +00:00
|
|
|
{ #category : #operation }
|
|
|
|
Markdown >> extractYAMLMetadata [
|
|
|
|
self detectYAMLMetadata ifFalse: [ ^ nil ].
|
|
|
|
^ self lines copyFrom: 2 to: (self locateYAMLMetadataClosing)
|
|
|
|
]
|
|
|
|
|
2020-07-10 22:52:16 +00:00
|
|
|
{ #category : #accessing }
|
|
|
|
Markdown >> file [
|
|
|
|
^ file
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
Markdown >> file: aFileReference [
|
|
|
|
"I store the origen/destination of the Markdown contents."
|
|
|
|
file := aFileReference
|
|
|
|
]
|
|
|
|
|
2019-06-18 11:26:52 +00:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
Markdown >> fromFile: aFileReference [
|
|
|
|
self contents: aFileReference contents.
|
2020-07-10 22:52:16 +00:00
|
|
|
self file: aFileReference
|
2019-06-18 11:26:52 +00:00
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
2019-06-18 11:26:52 +00:00
|
|
|
Markdown >> lines [
|
|
|
|
^ self contents lines.
|
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
2019-06-18 11:26:52 +00:00
|
|
|
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 |
|
2020-07-03 23:51:45 +00:00
|
|
|
(currentLine beginsWith: self class yamlMetadataDelimiter) ifTrue: [ result := i ]].
|
2019-06-18 11:26:52 +00:00
|
|
|
^ result
|
2020-07-03 23:51:45 +00:00
|
|
|
|
2019-06-18 11:26:52 +00:00
|
|
|
]
|
|
|
|
|
2020-07-10 22:52:16 +00:00
|
|
|
{ #category : #accessing }
|
|
|
|
Markdown >> metadata [
|
2020-10-10 19:35:47 +00:00
|
|
|
|
|
|
|
^ NeoJSONReader fromString: self exportMetadataAsJson.
|
2020-07-10 22:52:16 +00:00
|
|
|
]
|
|
|
|
|
2020-07-03 23:51:45 +00:00
|
|
|
{ #category : #utilities }
|
2019-06-18 11:26:52 +00:00
|
|
|
Markdown >> startsWithYAMLMetadataDelimiter [
|
2020-07-03 23:51:45 +00:00
|
|
|
^ self lines first beginsWith: self class yamlMetadataDelimiter
|
2019-06-18 11:26:52 +00:00
|
|
|
|
|
|
|
]
|
2020-07-03 23:51:45 +00:00
|
|
|
|
|
|
|
{ #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.
|
|
|
|
]
|