From 01a68d562cc574147235418bc436689e99e1b71c Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Fri, 20 Oct 2023 09:22:06 -0500 Subject: [PATCH] Importating legacy Grafoscopio notebooks: dealing with nil creation/modification timestamps. --- src/MiniDocs/LeDatabase.extension.st | 32 +++++++++++++++++++-- src/MiniDocs/OrderedDictionary.extension.st | 13 +++++++++ src/MiniDocs/XMLElement.extension.st | 30 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/MiniDocs/OrderedDictionary.extension.st create mode 100644 src/MiniDocs/XMLElement.extension.st diff --git a/src/MiniDocs/LeDatabase.extension.st b/src/MiniDocs/LeDatabase.extension.st index 4225660..6a9dfbc 100644 --- a/src/MiniDocs/LeDatabase.extension.st +++ b/src/MiniDocs/LeDatabase.extension.st @@ -23,14 +23,14 @@ LeDatabase >> addPageFromMarkdeep: markdeepDocTree withRemote: externalDocLocati | remoteMetadata divSnippets snippets page | divSnippets := (markdeepDocTree xpath: '//div[@st-class]') asOrderedCollection collect: [ :xmlElement | xmlElement postCopy ]. - self sanitizeMarkdeepSnippets: divSnippets. + remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree. + divSnippets := self sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata. snippets := divSnippets collect: [ :xmlElement | (xmlElement attributes at: 'st-class') = 'LeTextSnippet' ifTrue: [ LeTextSnippet new contentFrom: xmlElement ] ifFalse: [ (xmlElement attributes at: 'st-class') = 'LePharoSnippet' ifTrue: [ LePharoSnippet new contentFrom: xmlElement ] ] ]. - remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree. page := LePage new title: (remoteMetadata at: 'title'); basicUid: (UUID fromString36: (remoteMetadata at: 'id')); @@ -181,3 +181,31 @@ LeDatabase >> options [ ^ options ] + +{ #category : #'*MiniDocs' } +LeDatabase >> previewSanitizedPageFromMarkdeep: markdeepDocTree withRemote: externalDocLocation [ + | remoteMetadata divSnippets divSnippetsSanitized | + divSnippets := (markdeepDocTree xpath: '//div[@st-class]') asOrderedCollection + collect: [ :xmlElement | xmlElement postCopy ]. + remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree. + divSnippetsSanitized := self sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata. + ^ { divSnippets . divSnippetsSanitized . remoteMetadata } +] + +{ #category : #'*MiniDocs' } +LeDatabase >> sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata [ + ^ divSnippets collectWithIndex: [:markdeepDiv :i | | snippetData creationTime modificationTime | + snippetData := markdeepDiv asSnippetDictionary. + creationTime := snippetData at: 'created'. + modificationTime := snippetData at: 'modified'. + (creationTime = 'nil' and: [ modificationTime ~= 'nil' ]) + ifTrue: [ snippetData redefineTimestampsBefore: modificationTime ]. + (creationTime = 'nil' and: [ modificationTime = 'nil' ]) + ifTrue: [ | timeDiff | + timeDiff := divSnippets size - i. "Suggesting that last snippets were modified after the first ones." + modificationTime := (remoteMetadata at: 'created') asDateAndTime - timeDiff seconds. + snippetData redefineTimestampsBefore: modificationTime. + ]. + snippetData. + ] +] diff --git a/src/MiniDocs/OrderedDictionary.extension.st b/src/MiniDocs/OrderedDictionary.extension.st new file mode 100644 index 0000000..443967f --- /dev/null +++ b/src/MiniDocs/OrderedDictionary.extension.st @@ -0,0 +1,13 @@ +Extension { #name : #OrderedDictionary } + +{ #category : #'*MiniDocs' } +OrderedDictionary >> redefineTimestampsBefore: dateAndTime [ + self at: 'modified' put: dateAndTime asDateAndTime. + self at: 'created' put: dateAndTime asDateAndTime - 1 second. + self at: 'errata' ifAbsentPut: [ | initErrata | + initErrata := OrderedCollection new. + initErrata add: 'Modified timestamps: created/modified date and time were replaced instead of nil values. See "origin" key for more traceability info.'. + initErrata + ]. + +] diff --git a/src/MiniDocs/XMLElement.extension.st b/src/MiniDocs/XMLElement.extension.st new file mode 100644 index 0000000..704bc95 --- /dev/null +++ b/src/MiniDocs/XMLElement.extension.st @@ -0,0 +1,30 @@ +Extension { #name : #XMLElement } + +{ #category : #'*MiniDocs' } +XMLElement >> asSnippetDictionary [ + | response | + response := STON fromString: (self attributes at: 'st-data'). + response at: 'className' put: (self attributes at: 'st-class'). + response at: 'content' put: self sanitizedContent. + ^ response +] + +{ #category : #'*MiniDocs' } +XMLElement >> sanitizedContent [ + | className sanitizedText | + className := self attributes at: 'st-class'. + (className = 'LeTextSnippet') ifTrue: [ + sanitizedText := self contentString. + sanitizedText := sanitizedText allButFirst. + sanitizedText := sanitizedText allButLast. + ]. + (className = 'LePharoSnippet') ifTrue: [ | joinedText | + sanitizedText := self contentString lines. + sanitizedText := sanitizedText copyFrom: 4 to: sanitizedText size -2. + joinedText := '' writeStream. + sanitizedText do: [ :line | joinedText nextPutAll: line; nextPut: Character lf ]. + sanitizedText := joinedText contents allButLast. + ]. + ^ sanitizedText + +]