More modular code from Lepiter snippets conversion to/from ordered dictionaries.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2023-10-20 12:59:17 -05:00
parent 01a68d562c
commit a84ead51e1
4 changed files with 53 additions and 49 deletions

View File

@ -20,17 +20,13 @@ LeDatabase >> addPageCopy: aLePage [
{ #category : #'*MiniDocs' }
LeDatabase >> addPageFromMarkdeep: markdeepDocTree withRemote: externalDocLocation [
| remoteMetadata divSnippets snippets page |
| remoteMetadata divSnippets dataSnippets snippets page |
divSnippets := (markdeepDocTree xpath: '//div[@st-class]') asOrderedCollection
collect: [ :xmlElement | xmlElement postCopy ].
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.
remoteMetadata at: 'origin' put: externalDocLocation.
dataSnippets := self sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata.
snippets := dataSnippets collect: [ :each | each asLepiterSnippet ].
page := LePage new
title: (remoteMetadata at: 'title');
basicUid: (UUID fromString36: (remoteMetadata at: 'id'));
@ -187,24 +183,33 @@ LeDatabase >> previewSanitizedPageFromMarkdeep: markdeepDocTree withRemote: exte
| remoteMetadata divSnippets divSnippetsSanitized |
divSnippets := (markdeepDocTree xpath: '//div[@st-class]') asOrderedCollection
collect: [ :xmlElement | xmlElement postCopy ].
remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree.
remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree.
remoteMetadata at: 'origin' put: externalDocLocation.
divSnippetsSanitized := self sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata.
^ { divSnippets . divSnippetsSanitized . remoteMetadata }
]
{ #category : #'*MiniDocs' }
LeDatabase >> sanitizeMarkdeepSnippets: divSnippets withMetadata: remoteMetadata [
^ divSnippets collectWithIndex: [:markdeepDiv :i | | snippetData creationTime modificationTime |
^ divSnippets collectWithIndex: [:markdeepDiv :i | | snippetData creationTime modificationTime timestampWarning |
snippetData := markdeepDiv asSnippetDictionary.
creationTime := snippetData at: 'created'.
modificationTime := snippetData at: 'modified'.
timestampWarning := [:timestamp | 'Modified timestamps: ', timestamp ,' date and time was replaced instead of nil value. See "origin" key for more traceability info.'].
(creationTime = 'nil' and: [ modificationTime ~= 'nil' ])
ifTrue: [ snippetData redefineTimestampsBefore: modificationTime ].
ifTrue: [
snippetData redefineTimestampsBefore: modificationTime.
snippetData addErrata: (timestampWarning value: 'creation').
snippetData at: 'origin' put: (remoteMetadata at: 'origin').
].
(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 addErrata: (timestampWarning value: 'creation').
snippetData addErrata: (timestampWarning value: 'modification').
snippetData at: 'origin' put: (remoteMetadata at: 'origin').
].
snippetData.
]

View File

@ -11,19 +11,7 @@ LePharoSnippet >> contentAsStringCustomized [
{ #category : #'*MiniDocs' }
LePharoSnippet >> contentFrom: markdeepDiv [
| sanitizedStringText metadata joinedText |
metadata := STON fromString: (markdeepDiv attributes at: 'st-data').
sanitizedStringText := markdeepDiv contentString lines.
sanitizedStringText := sanitizedStringText copyFrom: 4 to: sanitizedStringText size -2.
joinedText := '' writeStream.
sanitizedStringText do: [ :line | joinedText nextPutAll: line; nextPut: Character lf ].
self code: joinedText contents allButLast;
uid: (LeUID new uidString: (metadata at: 'id'));
parent: (metadata at: 'parent');
createTime: (LeTime new time: ((metadata at: 'created')asDateAndTime));
editTime: (LeTime new time: ((metadata at: 'modified') asDateAndTime));
editEmail: (metadata at: 'modifier');
createEmail: (metadata at: 'creator')
^ markdeepDiv asSnippetDictionary asLepiterSnippet
]
{ #category : #'*MiniDocs' }

View File

@ -16,24 +16,7 @@ LeTextSnippet >> asLePage [
{ #category : #'*MiniDocs' }
LeTextSnippet >> contentFrom: markdeepDiv [
| sanitizedStringText metadata creationTime modificationTime |
metadata := STON fromString: (markdeepDiv attributes at: 'st-data').
self uid: (LeUID new uidString: (metadata at: 'id')).
"creationTime is needed to deal with empty values from Grafoscopio migrated notebooks."
creationTime := metadata at: 'created'.
modificationTime := (metadata at: 'modified') asDateAndTime.
(creationTime isNil or: [ creationTime = 'nil']) ifTrue: [
creationTime := modificationTime - 1 second.
self addErrata: 'Older: created before it appears.'].
sanitizedStringText := markdeepDiv contentString.
sanitizedStringText := sanitizedStringText allButFirst.
sanitizedStringText := sanitizedStringText allButLast.
self string: sanitizedStringText;
parent: (metadata at: 'parent');
createTime: (LeTime new time: creationTime asDateAndTime);
editTime: (LeTime new time: modificationTime);
editEmail: (metadata at: 'modifier');
createEmail: (metadata at: 'creator')
^ markdeepDiv asSnippetDictionary asLepiterSnippet
]
{ #category : #'*MiniDocs' }

View File

@ -1,13 +1,41 @@
Extension { #name : #OrderedDictionary }
{ #category : #'*MiniDocs' }
OrderedDictionary >> addErrata: noteString [
self errata add: noteString
]
{ #category : #'*MiniDocs' }
OrderedDictionary >> asLepiterSnippet [
| response |
self at: 'className' ifAbsent: [ ^ nil ].
(self at: 'className') = 'LeTextSnippet'
ifTrue: [
response := LeTextSnippet new.
response string: (self at: 'content')
].
(self at: 'className') = 'LePharoSnippet'
ifTrue: [
response := LePharoSnippet new.
response code: (self at: 'content')
].
response
uid: (LeUID new uidString: (self at: 'id'));
parent: (self at: 'parent');
createTime: (LeTime new time: ((self at: 'created')asDateAndTime));
editTime: (LeTime new time: ((self at: 'modified') asDateAndTime));
editEmail: (self at: 'modifier');
createEmail: (self at: 'creator').
^ response
]
{ #category : #'*MiniDocs' }
OrderedDictionary >> errata [
^ self at: 'errata' ifAbsentPut: [ OrderedCollection new]
]
{ #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
].
]