Improving data import/export and dependencies.

This commit is contained in:
Offray Vladimir Luna Cárdenas 2022-01-18 13:54:14 -05:00
parent 87d66ddeeb
commit 9d5282275b
3 changed files with 91 additions and 29 deletions

View File

@ -13,8 +13,9 @@ BaselineOfTiddlyWikiPharo >> baseline: spec [
"Dependencies" "Dependencies"
spec baseline: 'ZTimestamp' with: [ spec repository: 'github://svenvc/ztimestamp/repository' ]. spec baseline: 'ZTimestamp' with: [ spec repository: 'github://svenvc/ztimestamp/repository' ].
self xmlParserHTML: spec. self xmlParserHTML: spec.
spec baseline: 'Mustache' with: [ spec repository: 'github://noha/mustache/repository' ].
"Packages" "Packages"
spec package: 'TiddlyWiki' with: [ spec requires: #('ZTimestamp' 'XMLParserHTML') ] spec package: 'TiddlyWiki' with: [ spec requires: #('ZTimestamp' 'XMLParserHTML' 'Mustache') ]
] ]
] ]

View File

@ -110,7 +110,7 @@ Tiddler >> caption: anObject [
{ #category : #accessing } { #category : #accessing }
Tiddler >> created [ Tiddler >> created [
^ created ifNil: [ created := self class nowLocal ] ^ created asZTimestamp ifNil: [ created := self class nowLocal ]
] ]
{ #category : #accessing } { #category : #accessing }
@ -145,8 +145,9 @@ Tiddler >> exportJSONFile [
{ #category : #accessing } { #category : #accessing }
Tiddler >> exportSTONFile [ Tiddler >> exportSTONFile [
| stonFile output | | stonFile output dashedTitle |
stonFile := self wiki file parent / 'tiddlers' / (self title asDashedLowercase, '.', self created asString, '.ston') . dashedTitle := '-' join: (self title substrings collect: [ :each | each ]).
stonFile := self wiki file parent / 'tiddlers' / (dashedTitle, '.', self created asString, '.ston').
^ MarkupFile exportAsFileOn: stonFile containing: self asStonStringPretty ^ MarkupFile exportAsFileOn: stonFile containing: self asStonStringPretty
] ]
@ -171,10 +172,16 @@ Tiddler >> fromDictionary: aDictionary [
bag: (aDictionary at: 'bag' ifAbsentPut: [ nil ]); bag: (aDictionary at: 'bag' ifAbsentPut: [ nil ]);
list: (aDictionary at: 'list' ifAbsentPut: [ nil ]); list: (aDictionary at: 'list' ifAbsentPut: [ nil ]);
revision: (aDictionary at: 'revision' ifAbsentPut: [ nil ]). revision: (aDictionary at: 'revision' ifAbsentPut: [ nil ]).
customKeys := aDictionary keys copyWithoutAll: (self class instanceVariables collect: [ :each | each name ]). customKeys := aDictionary keys
copyWithoutAll: (self class instanceVariables collect: [ :each | each name ]).
customKeys ifEmpty: [ ^ self ]. customKeys ifEmpty: [ ^ self ].
customKeys do: [:key | customKeys do: [:key | | valueTemp |
self customFields at: key put: (aDictionary at: key) ]. valueTemp := aDictionary at: key.
valueTemp class = Array
ifTrue: [ self customFields at: key put: (self tiddlersListFrom: valueTemp) ]
ifFalse: [ self customFields at: key put: valueTemp ].
valueTemp class
].
] ]
{ #category : #'instance creation' } { #category : #'instance creation' }
@ -270,7 +277,7 @@ Tiddler >> markdownLinksAsWikiText [
{ #category : #accessing } { #category : #accessing }
Tiddler >> modified [ Tiddler >> modified [
^ modified ^ modified asZTimestamp
] ]
{ #category : #accessing } { #category : #accessing }
@ -343,6 +350,18 @@ Tiddler >> text: anObject [
text := anObject text := anObject
] ]
{ #category : #accessing }
Tiddler >> tiddlersListFrom: anArray [
| output |
output := '' writeStream.
anArray doWithIndex: [:each :i |
output nextPutAll: '[[', each asString, ']]'.
i = anArray size ifFalse: [ output nextPutAll: Character space asString ].
].
^ output contents.
]
{ #category : #accessing } { #category : #accessing }
Tiddler >> title [ Tiddler >> title [

View File

@ -8,10 +8,11 @@ Class {
#name : #TiddlyWiki, #name : #TiddlyWiki,
#superclass : #Object, #superclass : #Object,
#instVars : [ #instVars : [
'tiddlers', 'name',
'file', 'file',
'remote', 'remote',
'name' 'jsonFile',
'tiddlers'
], ],
#category : #'TiddlyWiki-Model' #category : #'TiddlyWiki-Model'
} }
@ -57,26 +58,46 @@ TiddlyWiki >> configFile [
] ]
{ #category : #accessing }
TiddlyWiki >> contentTiddlers [
^ self tiddlers copyWithoutAll: self shadow
]
{ #category : #accessing }
TiddlyWiki >> exportJSONFile [
| docTree rawJsonTiddlers |
self htmlFileExists.
docTree := XMLHTMLParser parse: self file contents.
rawJsonTiddlers := (docTree xpath: '//script[@class="tiddlywiki-tiddler-store"]') stringValue.
^ MarkupFile exportAsFileOn: self jsonFile containing: rawJsonTiddlers
]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> exportJSONSubtiddlers: subtiddlersCollection [ TiddlyWiki >> exportJSONSubtiddlers: subtiddlersCollection [
^ MarkupFile exportAsFileOn: self file parent / 'subtiddlers.json' containing: (self jsonSubtiddlers: subtiddlersCollection) ^ MarkupFile exportAsFileOn: self file parent / 'subtiddlers.json' containing: (self jsonSubtiddlers: subtiddlersCollection)
] ]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> exportSTONFile [ TiddlyWiki >> exportSTONFiles [
| stonFile output wikiTemp | | stonFile wikiTemp shadowFile |
self tiddlersJSONFile self tiddlersJSONFile
ifNil: [ ifNil: [
self inform: 'No JSON Tiddlers file found. If you have one, please provide its location'. self inform: 'No JSON Tiddlers file found. If you have one, please provide its location'.
stonFile := FileLocator temp / 'tiddlers.ston' ] stonFile := FileLocator temp / 'tiddlers.ston' ]
ifNotNil: [ stonFile := self tiddlersJSONFile withoutExtension, 'ston' ]. ifNotNil: [ stonFile := self tiddlersJSONFile withoutExtension, 'ston' ].
output := '' writeStream. shadowFile := self file parent / 'tiddlers' / '_shadow.ston'.
(STON writer on: output ) wikiTemp := self copy.
newLine: String lf; wikiTemp tiddlers: self contentTiddlers.
prettyPrint: true; wikiTemp := wikiTemp withoutImages.
keepNewLines: true; wikiTemp := wikiTemp withoutPDFs.
nextPut: self withoutImages. GrafoscopioUtils exportAsSton: self shadow on: shadowFile.
^ MarkupFile exportAsFileOn: stonFile containing:output contents ^ GrafoscopioUtils exportAsSton: wikiTemp on: stonFile
]
{ #category : #accessing }
TiddlyWiki >> exportSTONTiddlers: aCollection [
aCollection do: [:each | each exportSTONFile ]
] ]
{ #category : #accessing } { #category : #accessing }
@ -108,6 +129,14 @@ TiddlyWiki >> fromUrl: anUrlString [
self fromDictionary: tiddlersDictionary self fromDictionary: tiddlersDictionary
] ]
{ #category : #accessing }
TiddlyWiki >> htmlFileExists [
self file ifNil: [
self inform: 'No TiddlyWiki HTML file found. If you have one, please provide its location.'.
^ nil
].
]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> importJSONFile [ TiddlyWiki >> importJSONFile [
@ -122,6 +151,19 @@ TiddlyWiki >> importJSONFile [
self fromDictionary: tiddlersDict self fromDictionary: tiddlersDict
] ]
{ #category : #accessing }
TiddlyWiki >> jsonFile [
^ jsonFile ifNil: [
self htmlFileExists.
jsonFile := file parent / 'tiddlers.json'.]
]
{ #category : #accessing }
TiddlyWiki >> jsonFile: aFileLocator [
"I contain the tiddlers representation of the wiki data in JSON format."
jsonFile := aFileLocator
]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> jsonSubtiddlers: subtiddlersCollection [ TiddlyWiki >> jsonSubtiddlers: subtiddlersCollection [
| subtiddlersDict | | subtiddlersDict |
@ -196,6 +238,11 @@ TiddlyWiki >> remote: aUrlString [
remote := aUrlString asZnUrl remote := aUrlString asZnUrl
] ]
{ #category : #accessing }
TiddlyWiki >> shadow [
^ self tiddlers select: [:tiddler | tiddler title beginsWith: '$:/']
]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> taggedWith: aTag [ TiddlyWiki >> taggedWith: aTag [
^ self tiddlers select: [:tiddler | ^ self tiddlers select: [:tiddler |
@ -217,13 +264,8 @@ TiddlyWiki >> tiddlers: anOrderedCollection [
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> tiddlersJSONFile [ TiddlyWiki >> tiddlersJSONFile [
| jsonFile |
self file ifNil: [ self jsonFile exists ifFalse: [
self inform: 'No TiddlyWiki HTML file found. If you have one, please provide its location.'.
^ nil
].
jsonFile := file parent / 'tiddlers.json'.
jsonFile exists ifFalse: [
self inform: 'You need to export tiddlers as JSON from TiddlyWiki and locate it in the same folder as the HTML file'. self inform: 'You need to export tiddlers as JSON from TiddlyWiki and locate it in the same folder as the HTML file'.
^ nil ^ nil
]. ].
@ -236,7 +278,7 @@ TiddlyWiki >> tiddlersJSONUrl [
] ]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> withoutApplicationType: application [ TiddlyWiki >> withoutContentType: application [
| filteredTiddlers tempWiki | | filteredTiddlers tempWiki |
filteredTiddlers := self tiddlers reject: [:tiddler | tiddler type isNotNil and: [tiddler type beginsWith: application] ]. filteredTiddlers := self tiddlers reject: [:tiddler | tiddler type isNotNil and: [tiddler type beginsWith: application] ].
tempWiki := self copy tempWiki := self copy
@ -247,10 +289,10 @@ TiddlyWiki >> withoutApplicationType: application [
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> withoutImages [ TiddlyWiki >> withoutImages [
^ self withoutApplicationType: 'image/' ^ self withoutContentType: 'image/'
] ]
{ #category : #accessing } { #category : #accessing }
TiddlyWiki >> withoutPDFs [ TiddlyWiki >> withoutPDFs [
^ self withoutApplicationType: 'application/pdf' ^ self withoutContentType: 'application/pdf'
] ]