From 9d5282275b8659131241124eb637aefdf0e5315a Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Tue, 18 Jan 2022 13:54:14 -0500 Subject: [PATCH] Improving data import/export and dependencies. --- .../BaselineOfTiddlyWikiPharo.class.st | 3 +- repository/TiddlyWiki/Tiddler.class.st | 33 ++++++-- repository/TiddlyWiki/TiddlyWiki.class.st | 84 ++++++++++++++----- 3 files changed, 91 insertions(+), 29 deletions(-) diff --git a/repository/BaselineOfTiddlyWikiPharo/BaselineOfTiddlyWikiPharo.class.st b/repository/BaselineOfTiddlyWikiPharo/BaselineOfTiddlyWikiPharo.class.st index 67a5dda..827add4 100644 --- a/repository/BaselineOfTiddlyWikiPharo/BaselineOfTiddlyWikiPharo.class.st +++ b/repository/BaselineOfTiddlyWikiPharo/BaselineOfTiddlyWikiPharo.class.st @@ -13,8 +13,9 @@ BaselineOfTiddlyWikiPharo >> baseline: spec [ "Dependencies" spec baseline: 'ZTimestamp' with: [ spec repository: 'github://svenvc/ztimestamp/repository' ]. self xmlParserHTML: spec. + spec baseline: 'Mustache' with: [ spec repository: 'github://noha/mustache/repository' ]. "Packages" - spec package: 'TiddlyWiki' with: [ spec requires: #('ZTimestamp' 'XMLParserHTML') ] + spec package: 'TiddlyWiki' with: [ spec requires: #('ZTimestamp' 'XMLParserHTML' 'Mustache') ] ] ] diff --git a/repository/TiddlyWiki/Tiddler.class.st b/repository/TiddlyWiki/Tiddler.class.st index 2bbdd36..6bf6e71 100644 --- a/repository/TiddlyWiki/Tiddler.class.st +++ b/repository/TiddlyWiki/Tiddler.class.st @@ -110,7 +110,7 @@ Tiddler >> caption: anObject [ { #category : #accessing } Tiddler >> created [ - ^ created ifNil: [ created := self class nowLocal ] + ^ created asZTimestamp ifNil: [ created := self class nowLocal ] ] { #category : #accessing } @@ -145,8 +145,9 @@ Tiddler >> exportJSONFile [ { #category : #accessing } Tiddler >> exportSTONFile [ - | stonFile output | - stonFile := self wiki file parent / 'tiddlers' / (self title asDashedLowercase, '.', self created asString, '.ston') . + | stonFile output dashedTitle | + 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 ] @@ -171,10 +172,16 @@ Tiddler >> fromDictionary: aDictionary [ bag: (aDictionary at: 'bag' ifAbsentPut: [ nil ]); list: (aDictionary at: 'list' 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 do: [:key | - self customFields at: key put: (aDictionary at: key) ]. + customKeys do: [:key | | valueTemp | + 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' } @@ -270,7 +277,7 @@ Tiddler >> markdownLinksAsWikiText [ { #category : #accessing } Tiddler >> modified [ - ^ modified + ^ modified asZTimestamp ] { #category : #accessing } @@ -343,6 +350,18 @@ Tiddler >> 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 } Tiddler >> title [ diff --git a/repository/TiddlyWiki/TiddlyWiki.class.st b/repository/TiddlyWiki/TiddlyWiki.class.st index 4ed243b..ec886a4 100644 --- a/repository/TiddlyWiki/TiddlyWiki.class.st +++ b/repository/TiddlyWiki/TiddlyWiki.class.st @@ -8,10 +8,11 @@ Class { #name : #TiddlyWiki, #superclass : #Object, #instVars : [ - 'tiddlers', + 'name', 'file', 'remote', - 'name' + 'jsonFile', + 'tiddlers' ], #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 } TiddlyWiki >> exportJSONSubtiddlers: subtiddlersCollection [ ^ MarkupFile exportAsFileOn: self file parent / 'subtiddlers.json' containing: (self jsonSubtiddlers: subtiddlersCollection) ] { #category : #accessing } -TiddlyWiki >> exportSTONFile [ - | stonFile output wikiTemp | +TiddlyWiki >> exportSTONFiles [ + | stonFile wikiTemp shadowFile | self tiddlersJSONFile ifNil: [ self inform: 'No JSON Tiddlers file found. If you have one, please provide its location'. stonFile := FileLocator temp / 'tiddlers.ston' ] ifNotNil: [ stonFile := self tiddlersJSONFile withoutExtension, 'ston' ]. - output := '' writeStream. - (STON writer on: output ) - newLine: String lf; - prettyPrint: true; - keepNewLines: true; - nextPut: self withoutImages. - ^ MarkupFile exportAsFileOn: stonFile containing:output contents + shadowFile := self file parent / 'tiddlers' / '_shadow.ston'. + wikiTemp := self copy. + wikiTemp tiddlers: self contentTiddlers. + wikiTemp := wikiTemp withoutImages. + wikiTemp := wikiTemp withoutPDFs. + GrafoscopioUtils exportAsSton: self shadow on: shadowFile. + ^ GrafoscopioUtils exportAsSton: wikiTemp on: stonFile + +] + +{ #category : #accessing } +TiddlyWiki >> exportSTONTiddlers: aCollection [ + aCollection do: [:each | each exportSTONFile ] ] { #category : #accessing } @@ -108,6 +129,14 @@ TiddlyWiki >> fromUrl: anUrlString [ 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 } TiddlyWiki >> importJSONFile [ @@ -122,6 +151,19 @@ TiddlyWiki >> importJSONFile [ 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 } TiddlyWiki >> jsonSubtiddlers: subtiddlersCollection [ | subtiddlersDict | @@ -196,6 +238,11 @@ TiddlyWiki >> remote: aUrlString [ remote := aUrlString asZnUrl ] +{ #category : #accessing } +TiddlyWiki >> shadow [ + ^ self tiddlers select: [:tiddler | tiddler title beginsWith: '$:/'] +] + { #category : #accessing } TiddlyWiki >> taggedWith: aTag [ ^ self tiddlers select: [:tiddler | @@ -217,13 +264,8 @@ TiddlyWiki >> tiddlers: anOrderedCollection [ { #category : #accessing } TiddlyWiki >> tiddlersJSONFile [ - | jsonFile | - self file ifNil: [ - 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 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'. ^ nil ]. @@ -236,7 +278,7 @@ TiddlyWiki >> tiddlersJSONUrl [ ] { #category : #accessing } -TiddlyWiki >> withoutApplicationType: application [ +TiddlyWiki >> withoutContentType: application [ | filteredTiddlers tempWiki | filteredTiddlers := self tiddlers reject: [:tiddler | tiddler type isNotNil and: [tiddler type beginsWith: application] ]. tempWiki := self copy @@ -247,10 +289,10 @@ TiddlyWiki >> withoutApplicationType: application [ { #category : #accessing } TiddlyWiki >> withoutImages [ - ^ self withoutApplicationType: 'image/' + ^ self withoutContentType: 'image/' ] { #category : #accessing } TiddlyWiki >> withoutPDFs [ - ^ self withoutApplicationType: 'application/pdf' + ^ self withoutContentType: 'application/pdf' ]