GrafoscopioUtils/repository/Grafoscopio-Utils/LePage.extension.st

125 lines
3.6 KiB
Smalltalk

Extension { #name : #LePage }
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> asMarkdeep [
| bodyStream markdeep |
bodyStream := '' writeStream.
self preorderTraversal do: [:snippet |
bodyStream nextPutAll: snippet asMarkdeep
].
markdeep := Markdeep new
title: self title;
body: bodyStream contents.
self metadata keysAndValuesDo: [:k :v |
markdeep head
add: '<meta name="', k, '" content="', v,'">';
yourself.
].
self metadata at: 'authors' ifPresent: [:author | markdeep metadata at: 'authors' put: author ].
self metadata at: 'version' ifPresent: [:version | markdeep metadata at: 'version' put: version ].
^ markdeep.
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> asMarkdeepFile [
| folder |
folder := self options at: 'storage' ifAbsent: [ FileLocator temp ].
^ self asMarkdeep exportAsFileOn: folder / self markdeepFileName
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> asMarkdeepInto: aFileLocator [
self preorderTraversal
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> detectSnippetWithUid: uidString [
"Answer a boolean indicating whether the supplied uid is present"
^ self preorderTraversal detect: [ :snippet | snippet uidString = uidString ] ifNone: [ ^ nil ]
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> fileName [
^ self title asDashedLowercase, '.', ((self uidString copyFrom: 1 to: 5 ) copyWithoutAll: '/'), '.md.html'
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> fromMarkdeepUrl: aString [
| docTree pageMetadata |
docTree := GrafoscopioUtils xmlFromUrl: aString.
pageMetadata := Markdeep new metadataFromXML: docTree.
self
basicUid: (pageMetadata at: 'id');
title: (pageMetadata at: 'title');
createTime: (pageMetadata at: 'created');
editTime: (pageMetadata at: 'modified');
createEmail: (pageMetadata at: 'creator');
editEmail: (pageMetadata at: 'modifier');
optionAt: 'metadata' put: pageMetadata.
self populateChildrenFrom: (docTree xpath: '//div')
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> markdeepFileName [
| sanitized |
sanitized := self title asDashedLowercase copyWithoutAll: #($/).
^ sanitized, '--',(self uidString copyFrom: 1 to: 5), '.md.html'.
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> markdeepTemporalFile [
^ FileLocator temp / self markdeepFileName.
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> metadata [
^ self options at: 'metadata' ifAbsentPut: [ self metadataInit]
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> metadataInit [
^ OrderedDictionary new
at: 'id' put: self uidString;
at: 'title' put: self contentAsString;
at: 'created' put: self createTime asString;
at: 'modified' put: self latestEditTime asString;
at: 'creator' put: self createEmail asString;
at: 'modifier' put: self editEmail asString;
yourself
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> options [
^ options
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> populateChildrenFrom: docTreeDivs [
docTreeDivs doWithIndex: [:div :i | | snippet parent |
snippet := LeSnippet fromMetaMarkdeep:div.
self children
addSnippet: snippet beforeIndex: i.
parent := self detectSnippetWithUid: (snippet metadata at: 'parent').
snippet parent: parent.
]
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> preorderTraversal [
| output |
output := OrderedCollection new.
self withDeepCollect: [:each | each allChildrenBreadthFirstDo: [:child | output add: child ] ].
^ output.
]
{ #category : #'*Grafoscopio-Utils-Core' }
LePage >> removeSnippetsMetadata [
self preorderTraversal do: [ :snippet | snippet options removeKey: 'metadata' ]
]