MiniDocs/src/MiniDocs/LeDatabase.extension.st

154 lines
4.8 KiB
Smalltalk
Raw Normal View History

2022-09-21 18:57:52 +00:00
Extension { #name : #LeDatabase }
{ #category : #'*MiniDocs' }
LeDatabase >> addPageFromMarkdeep: markdeepDocTree withRemote: externalDocLocation [
| remoteMetadata divSnippets snippets page |
divSnippets := (markdeepDocTree xpath: '//div[@st-class]')
asOrderedCollection collect: [ :xmlElement | xmlElement postCopy ].
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 ]
]
].
2022-09-22 02:15:11 +00:00
remoteMetadata := Markdeep new metadataFromXML: markdeepDocTree.
page := LePage new
title: (remoteMetadata at: 'title');
basicUid: (UUID fromString36: (remoteMetadata at: 'id'));
createTime: (LeTime new time: ((remoteMetadata at: 'created') asDateAndTime));
editTime: (LeTime new time: ((remoteMetadata at: 'modified') asDateAndTime));
latestEditTime: (LeTime new time: ((remoteMetadata at: 'modified') asDateAndTime));
createEmail: (LeEmail new email:(remoteMetadata at: 'creator'));
editEmail: (LeEmail new email:(remoteMetadata at: 'modifier')).
snippets do: [ :snippet | page addSnippet: snippet ].
page children do: [ :snippet |
(self hasBlockUID: (snippet uid))
2022-10-07 19:37:56 +00:00
ifTrue: [ | existingPage |
2022-10-09 01:59:57 +00:00
existingPage := self pages detect: [ :pageTemp | pageTemp includesSnippetUid: snippet uid ].
self importErrorMessage: existingPage.
^ self ]
ifFalse: [
snippet database: self.
self registerSnippet: snippet ] ].
self addPage: page.
^ page
2022-09-21 18:57:52 +00:00
]
2022-09-29 13:20:56 +00:00
{ #category : #'*MiniDocs' }
LeDatabase >> addPageFromMarkdeepUrl: aString [
| page |
page := self detectLocalPageForRemote: aString.
page
ifNotNil: [ :arg |
self importErrorMessage: page.
^ self ].
^ self addPageFromMarkdeep: (self docTreeForLink: aString) withRemote: aString
2022-09-29 13:20:56 +00:00
]
2022-09-21 18:57:52 +00:00
{ #category : #'*MiniDocs' }
LeDatabase >> detectLocalPageForRemote: markdeepDocUrl [
| markdeepHelper id remoteMetadata docTree |
markdeepHelper := Markdeep new.
2022-09-21 21:15:13 +00:00
docTree := self docTreeForLink: markdeepDocUrl.
2022-09-21 18:57:52 +00:00
remoteMetadata := markdeepHelper metadataFromXML: docTree.
2022-09-21 21:15:13 +00:00
id := remoteMetadata at: 'id' ifAbsent: [ nil ].
^ self pageWithID: id ifAbsent: [ ^ nil ].
]
{ #category : #'*MiniDocs' }
LeDatabase >> docTreeForLink: aString [
2022-09-22 02:15:11 +00:00
^ (XMLHTMLParser on: aString asUrl retrieveContents) parseDocument
2022-09-21 18:57:52 +00:00
]
2022-10-07 22:46:47 +00:00
{ #category : #'*MiniDocs' }
LeDatabase >> errorCardFor: errorMessage [
| keepButton overwriteButton backupButton errorMessageUI |
keepButton := BrButton new
aptitude: BrGlamorousButtonWithIconAndLabelAptitude;
beTinySize;
2022-10-09 01:59:57 +00:00
label: 'Keep existing local page';
2022-10-07 22:46:47 +00:00
icon: BrGlamorousVectorIcons cancel;
action: [ :aButton | ];
margin: (BlInsets left: 10).
overwriteButton := BrButton new
aptitude: BrGlamorousButtonWithIconAndLabelAptitude;
beTinySize;
2022-10-09 01:59:57 +00:00
label: 'Overwrite with remote page';
2022-10-07 22:46:47 +00:00
icon: BrGlamorousVectorIcons edit;
action: [ :aButton | ];
margin: (BlInsets left: 10).
backupButton := BrButton new
aptitude: BrGlamorousButtonWithIconAndLabelAptitude;
beTinySize;
2022-10-09 01:59:57 +00:00
label: 'Backup local page';
2022-10-07 22:46:47 +00:00
icon: BrGlamorousVectorIcons changes;
action: [ :aButton | ];
margin: (BlInsets left: 10).
errorMessageUI := BrEditor new
2022-10-09 01:59:57 +00:00
aptitude: BrGlamorousRegularEditorAptitude new ;
2022-10-07 22:46:47 +00:00
text: errorMessage;
vFitContent.
^ { errorMessageUI. keepButton. overwriteButton. backupButton }
]
{ #category : #'*MiniDocs' }
LeDatabase >> errors [
^ self optionAt: 'errors' ifAbsent: [ self optionAt: 'errors' put: Dictionary new ]
]
{ #category : #'*MiniDocs' }
LeDatabase >> gtViewErrorDetailsOn: aView [
<gtView>
^ aView explicit
title: 'Errors' translated;
priority: 5;
stencil: [
| container |
container := BlElement new
layout: BlFlowLayout new;
constraintsDo: [ :c |
c vertical fitContent.
c horizontal matchParent ];
padding: (BlInsets all: 10).
container
addChildren: (self errorCardFor: self errors associations first value)
"container addChild: self asCardElement "].
]
2022-09-21 18:57:52 +00:00
{ #category : #'*MiniDocs' }
LeDatabase >> importErrorMessage: page [
| message id |
id := page uidString.
message := String streamContents: [ :stream |
2022-09-21 18:57:52 +00:00
stream
2022-10-09 01:59:57 +00:00
nextPutAll: 'IMPORTATION ERROR: A page with
';
2022-09-21 18:57:52 +00:00
nextPut: Character lf;
nextPutAll: ' id: ' , id;
2022-09-21 18:57:52 +00:00
nextPut: Character lf;
nextPutAll: ' title: ' , page contentAsString;
nextPut: Character lf;
nextPut: Character lf;
2022-10-09 01:59:57 +00:00
nextPutAll: 'already exists in this database and includes overlapping contents';
nextPut: Character lf;
nextPutAll: 'with the page you are trying to import.';
2022-09-21 18:57:52 +00:00
nextPut: Character lf;
nextPut: Character lf;
nextPutAll:
2022-10-09 01:59:57 +00:00
'Please choose one of the following options to addres the issue:
' ].
self errors at: id put: message
]
{ #category : #'*MiniDocs' }
LeDatabase >> options [
^ options
2022-09-21 18:57:52 +00:00
]