1074 lines
30 KiB
Smalltalk
1074 lines
30 KiB
Smalltalk
"
|
|
An UbakyeNode is and administrator of all node operations in a tree.
|
|
|
|
Instance Variables
|
|
node: <Object>
|
|
|
|
node
|
|
- xxxxx
|
|
|
|
"
|
|
Class {
|
|
#name : #GrafoscopioNode,
|
|
#superclass : #Object,
|
|
#instVars : [
|
|
'header',
|
|
'headers',
|
|
'key',
|
|
'icon',
|
|
'body',
|
|
'tags',
|
|
'children',
|
|
'parent',
|
|
'node',
|
|
'level',
|
|
'nodesInPreorder',
|
|
'links',
|
|
'output'
|
|
],
|
|
#classInstVars : [
|
|
'clipboard'
|
|
],
|
|
#category : #'Grafoscopio-Model'
|
|
}
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode class >> cleanTreeRootReferences [
|
|
|
|
| ref |
|
|
clipboard ifNil: [ ^ self ].
|
|
clipboard children ifNil: [ ^ self ].
|
|
clipboard preorderTraversal allButFirstDo: [ :n |
|
|
ref := n.
|
|
n level - 1 timesRepeat: [ ref := ref parent ].
|
|
ref parent = clipboard parent ifTrue: [ ref parent: nil ]].
|
|
clipboard parent: nil.
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode class >> clipboard [
|
|
^ clipboard
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode class >> clipboard: anObject [
|
|
clipboard := anObject
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode class >> specialWords [
|
|
"I return a list of word that were used in the first versions of Grafoscopio to mark node
|
|
headers to indicate special ways to handle them and their node contents.
|
|
Previous versions of first notebooks stored in Grafoscopio using this convention should be
|
|
migrated to newer versions where tags are used for the same function with simpler code"
|
|
|
|
^ #('%config' '%abstract' '%invisible' '%idea' '%footnote' '%metadata' '%output' '%embed' '%item').
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> addLink: anUrl [
|
|
"anUrl is a string"
|
|
|
|
(self links includes: anUrl)
|
|
ifFalse: [ self links add: anUrl ]
|
|
|
|
]
|
|
|
|
{ #category : #adding }
|
|
GrafoscopioNode >> addNode: aNode [
|
|
"Adds the given node to the receivers collection of children, and sets this object as the parent
|
|
of the node"
|
|
"aNode parent = self ifTrue: [ ^ self ]."
|
|
self children add: aNode.
|
|
aNode level: (self level) + 1.
|
|
aNode parent: self.
|
|
^aNode
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> addNodeAfterMe [
|
|
"Adds a generic node after the given node so they become slibings of the same parent"
|
|
| genericNode |
|
|
genericNode := self class new header: 'newNode'; body: ''.
|
|
self parent children add: genericNode after: self.
|
|
genericNode parent: self parent.
|
|
genericNode level: self level.
|
|
^ genericNode
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> ancestors [
|
|
"I return a collection of all the nodes wich are ancestors of the receiver node"
|
|
| currentNode ancestors |
|
|
|
|
currentNode := self.
|
|
ancestors := OrderedCollection new.
|
|
[ currentNode parent notNil and: [ currentNode level > 0 ] ]
|
|
whileTrue: [
|
|
ancestors add: currentNode parent.
|
|
currentNode := currentNode parent].
|
|
ancestors := ancestors reversed.
|
|
^ ancestors
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> ancestorsAll [
|
|
"I return a collection of all the nodes wich are ancestors of the receiver node"
|
|
| currentNode ancestors |
|
|
|
|
currentNode := self.
|
|
ancestors := OrderedCollection new.
|
|
[ currentNode parent notNil and: [ currentNode level > 0 ] ]
|
|
whileTrue: [
|
|
ancestors add: currentNode parent.
|
|
currentNode := currentNode parent].
|
|
ancestors := ancestors reversed.
|
|
^ ancestors
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> ancestorsHeaders [
|
|
"Returns the headers of all the ancestors of the node.
|
|
Maybe this and 'headers' should be integrated, so both act on a collection of children instead of
|
|
having two separate methods"
|
|
| currentNode ancestors |
|
|
|
|
currentNode := self.
|
|
ancestors := OrderedCollection new.
|
|
(self level - 1)
|
|
timesRepeat: [
|
|
ancestors add: currentNode parent.
|
|
currentNode := currentNode parent.].
|
|
ancestors := ancestors reversed.
|
|
^ ancestors collect: [:ancestor | ancestor header ]
|
|
|
|
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> asMarkdown [
|
|
"I export children of the current node as pandoc markdown, using special nodes
|
|
accoding to tags.
|
|
Early version... tags processing should be vastly improved"
|
|
| markdownOutput |
|
|
|
|
markdownOutput := '' writeStream.
|
|
"self metadataAsYamlIn: markdownOutput."
|
|
(self preorderTraversal) do: [ :eachNode |
|
|
(eachNode level > 0)
|
|
ifTrue: [(eachNode hasAncestorTaggedAs: 'invisible')
|
|
| (eachNode tags includes: 'invisible')
|
|
ifFalse: [
|
|
markdownOutput nextPutAll: (eachNode markdownContent) ]]].
|
|
^ markdownOutput contents
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> asSton [
|
|
"Exports current tree as STON format"
|
|
| stonOutput |
|
|
|
|
stonOutput := '' writeStream.
|
|
self flatten.
|
|
stonOutput nextPutAll: (STON toStringPretty: self children).
|
|
^stonOutput contents
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> asText [
|
|
^ self body
|
|
]
|
|
|
|
{ #category : #initialization }
|
|
GrafoscopioNode >> becomeDefaultTestTree [
|
|
| node1 node2 node3 node4 |
|
|
self
|
|
level: 0;
|
|
header: 'Arbol principal'.
|
|
node1 := self class new
|
|
header: 'Markup';
|
|
body: 'I am <b>just a node with markup</b>';
|
|
tagAs: 'text';
|
|
links: 'temp.md';
|
|
level: 1.
|
|
node2 := self class new
|
|
header: '%output Code';
|
|
tagAs: 'código';
|
|
body: '(ConfigurationOfGrafoscopio>>#version14:) sourceCode'.
|
|
node3 := self class new
|
|
header: '%invisible';
|
|
tagAs: 'text';
|
|
body: '<i>Just testing</i>'.
|
|
node1 addNode: node3.
|
|
node4 := self class new
|
|
header: 'Something';
|
|
tagAs: 'text';
|
|
body: '<h1>else</h1>'.
|
|
node1 addNode: node4.
|
|
node1 addNode: node2.
|
|
self
|
|
addNode: node1.
|
|
]
|
|
|
|
{ #category : #initialization }
|
|
GrafoscopioNode >> becomeDefaultTree [
|
|
"I create a starting tree for all Grafoscopio notebooks with just one textual node as child."
|
|
| node1 |
|
|
self class new.
|
|
self
|
|
level: 0;
|
|
header: 'Arbol principal';
|
|
tagAs: 'código'.
|
|
node1 := self class new
|
|
header: 'Node 1';
|
|
body: '';
|
|
tagAs: 'text'.
|
|
self addNode: node1.
|
|
^ self
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> body [
|
|
"Returns the receivers body"
|
|
|
|
^ body
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> body: anObject [
|
|
"Sets the receivers body to the given object"
|
|
|
|
body := anObject
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> bodyAsCode [
|
|
"I return the node body with proper decorators added to show them as raw code"
|
|
| codeBody |
|
|
codeBody := '' writeStream.
|
|
codeBody
|
|
nextPutAll: '~~~{.numberLines}'; lf;
|
|
nextPutAll: (self body contents asString withInternetLineEndings); lf;
|
|
nextPutAll: '~~~'; lf; lf.
|
|
^ codeBody contents
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> bodyAsMarkdownInto: aStream [
|
|
"I export the header as markdown using the level inside the tree to determine hierarchy
|
|
and replacing all line endings to make them Internet friendly".
|
|
self embeddedNodes ifNotNil: [ aStream nextPutAll: (self embedNodes contents asString withInternetLineEndings); crlf; crlf].
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> checksum [
|
|
"I return the SHA1SUM of the current tree. I'm used to test changes on the contents
|
|
and for traceability of how the document tree is converted to other formats, as markdown."
|
|
^ (SHA1 new hashMessage: self root flatten asSton) hex
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> children [
|
|
"Returns the receivers list of children"
|
|
|
|
^ children ifNil: [ children := OrderedCollection new ]
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> children: aCollection [
|
|
"Sets the receivers children"
|
|
|
|
aCollection do: [:currentNode | currentNode parent: self ].
|
|
children := aCollection.
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> content [
|
|
"Returns the receivers body"
|
|
|
|
^ body
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> content: anObject [
|
|
"Sets the receivers body to the given object"
|
|
|
|
body := anObject
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> copyToClipboard [
|
|
self class clipboard: self subtreeCopy.
|
|
|
|
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode >> deleteReferencesToRoot: aRootNode [
|
|
|
|
| sparseTree |
|
|
sparseTree := self preorderTraversal.
|
|
]
|
|
|
|
{ #category : #movement }
|
|
GrafoscopioNode >> demote [
|
|
"I move the current node down in the hierachy, making it a children of its current previous
|
|
slibing"
|
|
|
|
| collection index predecessor |
|
|
collection := self parent children.
|
|
index := collection indexOf: self.
|
|
(index between: 2 and: collection size)
|
|
ifTrue: [ predecessor := collection before: self.
|
|
collection remove: self.
|
|
predecessor addNode: self]
|
|
|
|
]
|
|
|
|
{ #category : #'custom markup' }
|
|
GrafoscopioNode >> embedAll [
|
|
"This is just a previous part of the messy markDownContent. The %embed-all keyword should be revaluated.
|
|
By default a node embeds all its children. Any non-embedable content should be under a %invisible node"
|
|
"(temporalBody includesSubstring: '%embed-all')
|
|
ifFalse: [ ]
|
|
ifTrue: [
|
|
self embeddedNodes do: [ :each |
|
|
temporalBody := temporalBody copyReplaceAll: '%embed-all' with: (each body, (String with: Character cr),
|
|
'%embed-all')].
|
|
temporalBody := temporalBody copyReplaceAll: '%embed-all' with: '']"
|
|
|
|
]
|
|
|
|
{ #category : #'custom markup' }
|
|
GrafoscopioNode >> embedNodes [
|
|
"I find any ocurrence of '%embed a node header' in the body of a node and replace it
|
|
by the children which have such header.
|
|
Using embedded nodes is useful to change the order in which children appear into parents body,
|
|
while exporting"
|
|
| temporalBody |
|
|
temporalBody := self body.
|
|
self embeddedNodes ifNotNil: [ self embeddedNodes do: [ :each |
|
|
(each isTaggedAs: 'código')
|
|
ifFalse: [temporalBody := temporalBody copyReplaceAll: (each header) with: each body]
|
|
ifTrue: [temporalBody := temporalBody copyReplaceAll: (each header) with: each bodyAsCode]]].
|
|
^ temporalBody
|
|
]
|
|
|
|
{ #category : #'custom markup' }
|
|
GrafoscopioNode >> embeddedNodes [
|
|
^ self children select: [:each | each headerStartsWith: '%embed']
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> exportCodeBlockTo: aStream [
|
|
"I convert the content of a node taged as 'código' (code) as pandoc markdown and put it
|
|
into aStream."
|
|
aStream nextPutAll: ('~~~{.numberLines}'); lf.
|
|
aStream nextPutAll: (self body contents asString withInternetLineEndings); lf.
|
|
aStream nextPutAll: ('~~~'); lf;lf.
|
|
^aStream contents
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> exportCodeNodeTo: aStream [
|
|
"I convert the content of a node taged as 'código' (code) as pandoc markdown
|
|
and put it into aStream."
|
|
((self headerStartsWith: '%output') or: [ self headerStartsWith: '%metadata' ])
|
|
ifTrue: [ self exportCodeOutputTo: aStream ]
|
|
ifFalse: [ self exportCodeBlockTo: aStream ]
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> exportCodeOutputTo: aStream [
|
|
"I convert the output of a node taged as 'código' (code) as pandoc markdown and
|
|
put it into aStream."
|
|
(self headerStartsWith: '%metadata') ifTrue: [ ^ self ].
|
|
aStream nextPutAll: ('~~~{.numberLines}'); lf.
|
|
aStream nextPutAll: (self output asString withInternetLineEndings); lf.
|
|
aStream nextPutAll: ('~~~'); lf;lf.
|
|
^aStream contents
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> exportLaTeXCodeBlockTo: aStream [
|
|
"I convert the content of a node taged as 'código' (code) as pandoc markdown and put it
|
|
into aStream.
|
|
The code block is decorated with LaTeX commands for proper syntax highlighting using pygments.
|
|
Pdf exportation requires the installation of pygments and minted package for latex"
|
|
aStream nextPutAll: ('\begin{minted}{smalltalk}'); lf.
|
|
aStream nextPutAll: (self body contents asString withInternetLineEndings); lf.
|
|
aStream nextPutAll: '\end{minted}';lf;lf.
|
|
^aStream contents
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> exportPreambleTo: aStream [
|
|
"comment stating purpose of message"
|
|
| configDict |
|
|
(self header = '%config')
|
|
ifTrue: [
|
|
configDict := STON fromString: (self body).
|
|
aStream nextPutAll: 'title: ', (configDict at: 'title'); lf.
|
|
aStream nextPutAll: 'author: ', ((configDict at: 'author') at: 'given'), ' ', ((configDict at: 'author') at: 'family'); lf.
|
|
aStream nextPutAll: 'bibliography: ', (configDict at: 'bibliography'); lf.
|
|
aStream nextPutAll: 'abstract: ', '|'; lf; nextPutAll: (configDict at: 'abstract'); lf]
|
|
]
|
|
|
|
{ #category : #'as yet unclassified' }
|
|
GrafoscopioNode >> extractHtmlImages [
|
|
"comment stating purpose of message"
|
|
|imgSoup imgHost imgList folders|
|
|
|
|
imgList := Set new.
|
|
imgSoup := Soup fromString: self body.
|
|
imgHost:= self links last asUrl removeLastPathSegment.
|
|
folders:= Set new.
|
|
(imgSoup findAllTags: 'img') do: [ :each|| src |
|
|
src := (each attributeAt: 'src') asUrl.
|
|
(src host) ifNil: [ src := imgHost addPathSegments: src pathSegments ].
|
|
imgList add: src.
|
|
].
|
|
^imgList .
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode >> find: aString andReplaceWith: anotherString [
|
|
anotherString ifNil: [ ^ self ].
|
|
self body: ((self body) copyReplaceAll: aString with: anotherString)
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> flatten [
|
|
"I traverse the tree looking for node bodies containing 'Text' objects and transform them to
|
|
their string content, so space is saved and storage format is DVCS friendly while serializing
|
|
them to STON"
|
|
|
|
(self preorderTraversal) do: [ :eachNode |
|
|
(eachNode body class = Text)
|
|
ifTrue: [eachNode body: (eachNode body asString)]]
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> footnoteAsMarkdownInto: aStream [
|
|
"I export a node with %footnode in its header for valid Pandoc's markdown
|
|
and replace all line endings to make them Internet friendly.
|
|
Maybe I should include the condition about my own header, instead of leaving it to markdownCotent..."
|
|
aStream nextPutAll: ('[^',(self header copyReplaceAll: '%footnote ' with: ''),']: ' ); lf.
|
|
self body contents withInternetLineEndings
|
|
linesDo: [ :line | aStream nextPutAll: ' ', line; lf ].
|
|
aStream nextPutAll: String lf.
|
|
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> hasAncestorHeaderWith: aSpecialWord [
|
|
"Looks if the receptor node has an ancestor with a header with 'aSpecialWord' as the only or the first word"
|
|
|
|
^ (self ancestorsHeaders includes: aSpecialWord) | ((self ancestorsHeaders collect: [:eachHeader | (eachHeader findTokens: $ ) at: 1 ]) includes: aSpecialWord)
|
|
|
|
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> hasAncestorTaggedAs: aSpecialWord [
|
|
"Looks if the receptor node has an ancestor with a header with 'aSpecialWord' in its tags"
|
|
|
|
self ancestors detect: [:eachAncestor | eachAncestor tags includes: aSpecialWord ]
|
|
ifFound: [^true ]
|
|
ifNone: [^false ].
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> hasChildren [
|
|
(self children size > 0)
|
|
ifTrue: [ ^true ]
|
|
ifFalse: [ ^false ]
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> header [
|
|
"Returns the receiver header"
|
|
|
|
^ header
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> header: anObject [
|
|
"Sets the receivers header"
|
|
|
|
header := anObject
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> headerAsMarkdownInto: aStream [
|
|
"I export the header as markdown using the level inside the tree to determine hierarchy
|
|
and replacing all line endings to make them Internet friendly"
|
|
self level timesRepeat: [ aStream nextPutAll: '#' ].
|
|
aStream nextPutAll: ' '.
|
|
aStream nextPutAll: (self header copyReplaceTokens: #cr with: #lf); crlf; crlf.
|
|
]
|
|
|
|
{ #category : #'custom markup' }
|
|
GrafoscopioNode >> headerStartsWith: aString [
|
|
^ (self header findString: aString) = 1
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> headers [
|
|
"I returns the headers of the receiver children"
|
|
^ headers := self children collect: [:currentNode | currentNode header ]
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> htmlToMarkdown [
|
|
"I convert the node body from HTML format to Pandoc's Markdown."
|
|
| htmlFile markdownFile |
|
|
|
|
(self isTaggedAs: 'código' ) ifTrue: [ ^self ].
|
|
((self headerStartsWith: '%invisible') "or:[self hasAncestorHeaderWith: '%invisible']")
|
|
ifTrue: [ ^self ].
|
|
htmlFile := FileLocator temp asFileReference / 'body.html'.
|
|
htmlFile ensureDelete.
|
|
markdownFile := FileLocator temp asFileReference / 'body.markdown'.
|
|
markdownFile ensureDelete.
|
|
htmlFile writeStreamDo: [:out | out nextPutAll: self body ].
|
|
Smalltalk platformName = 'unix'
|
|
ifTrue: [ OSProcess
|
|
waitForCommand: 'pandoc -f html -t markdown --atx-headers ',
|
|
htmlFile fullName, ' -o ', markdownFile fullName ].
|
|
Smalltalk platformName = 'Win32'
|
|
ifTrue: [ WinProcess
|
|
createAndWaitForProcess: 'pandoc -f html -t markdown --atx-headers ',
|
|
htmlFile fullName, ' -o ', markdownFile fullName ].
|
|
self body: markdownFile contents.
|
|
htmlFile ensureDelete.
|
|
markdownFile ensureDelete.
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> htmlToMarkdownSubtree [
|
|
"I convert self and childern nodes body from HTML format to Pandoc's Markdown."
|
|
self preorderTraversal do: [ :each | each htmlToMarkdown ]
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> icon [
|
|
"Returns the receivers icon"
|
|
|
|
^icon
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> icon: aSymbol [
|
|
"Sets the receivers icon"
|
|
|
|
icon := aSymbol
|
|
]
|
|
|
|
{ #category : #importing }
|
|
GrafoscopioNode >> importHtmlLink [
|
|
"I take the last link and import its contents in node body. "
|
|
self links last asUrl host = 'ws.stfx.eu' ifTrue: [ ^ self ].
|
|
self
|
|
body: (ZnClient new get: self links last).
|
|
]
|
|
|
|
{ #category : #importing }
|
|
GrafoscopioNode >> importPlaygroundLink [
|
|
"I take the last link and import its contents in node body.
|
|
Last links should be hosted in http://zn.stfx.eu/"
|
|
self links last asUrl host = 'ws.stfx.eu' ifFalse: [ ^ self ].
|
|
self
|
|
body: (ZnClient new get: self links last);
|
|
tagAs: 'código'.
|
|
]
|
|
|
|
{ #category : #initialization }
|
|
GrafoscopioNode >> initialize [
|
|
"I create a empty new node"
|
|
|
|
super initialize.
|
|
self
|
|
header: 'newHeader';
|
|
tagAs: 'text';
|
|
body: '';
|
|
level: 0
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> isEmpty [
|
|
body ifNil: [ ^ true ] ifNotNil: [ ^ false ]
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> isTaggedAs: aString [
|
|
self tags ifEmpty: [ self tagAs: 'text' ].
|
|
^ self tags includes: aString
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> itemAsMarkdownInto: aStream [
|
|
"I export a node with %item in its header as valid Pandoc's markdown
|
|
and replace all line endings to make them Internet friendly.
|
|
Maybe I should include the condition about my own header, instead of leaving it to markdownContent..."
|
|
|
|
| lines |
|
|
lines := self body contents asString withInternetLineEndings lines.
|
|
lines ifEmpty: [ ^ self ].
|
|
aStream
|
|
nextPutAll: ' - ';
|
|
nextPutAll: lines first;
|
|
lf.
|
|
lines
|
|
allButFirstDo: [ :line |
|
|
aStream
|
|
nextPutAll: ' ';
|
|
nextPutAll: line;
|
|
lf ].
|
|
aStream nextPutAll: String lf
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> key [
|
|
"Returns a unique key identifying the receiver in the help system"
|
|
|
|
^key
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> key: aUniqueKey [
|
|
"Sets a unique key identifying the receiver in the help system"
|
|
|
|
key := aUniqueKey
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> lastLink [
|
|
self links ifNil: [ ^ '' ].
|
|
self links ifEmpty: [ ^ '' ].
|
|
^ self links last
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> lastNetLink [
|
|
^ self links detect: [ :l | l asZnUrl isURL ]
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> level [
|
|
"Returns the level of the node. See the setter message for details"
|
|
|
|
^level
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> level: anInteger [
|
|
"Sets the node level in a hierarchy. The only node with level 0 is the root node and from there levels increase
|
|
in 1 for its direct children, 2 for its grand children and so on. Silibings nodes has the same level"
|
|
|
|
level := anInteger
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> links [
|
|
"I model local or remote links that are associated to a particular node."
|
|
^ links ifNil: [ ^ links := OrderedCollection new ]
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> links: anObject [
|
|
self links add: anObject
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> linksToMarkdownFile [
|
|
"I detect if the links contains any reference to a file ending in '.md' or '.markdown'"
|
|
self links
|
|
ifNotNil: [
|
|
self links
|
|
detect: [:l | (l endsWith: '.md') or: [ l endsWith: '.markdown']]
|
|
ifFound: [ ^ true ]
|
|
ifNone: [^ false]].
|
|
^ false
|
|
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> localFilesLinks [
|
|
"I collect all the links that point to the local file system. Because is supposed that
|
|
links contains only references to remote URL or local files, anything that is not a URL is
|
|
treated as a loca file link."
|
|
^ self links collect: [ :l | l asZnUrl host isNil ]
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> margin [
|
|
"I define the same margin of the page used for PDF exportations"
|
|
|
|
^'2 cm'
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> margins [
|
|
"I define each individual margin of the page used for PDF exportations"
|
|
|
|
| margins |
|
|
margins := Dictionary new
|
|
add: 'top' -> '3 cm';
|
|
add: 'bottom' -> '3 cm';
|
|
add: 'left' -> '2 cm';
|
|
add: 'right' -> '2 cm';
|
|
yourself.
|
|
^ margins
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> markdownContent [
|
|
"I extract the markdown of a node using body as content, header as title and level as
|
|
hierarchical level of the title.
|
|
If special nodes types are present, that use %keywords in its header or body I convert them
|
|
into proper markup"
|
|
| markdownStream |
|
|
markdownStream := '' writeStream.
|
|
(self class specialWords includes: self header) not
|
|
& (self class specialWords includes: ((self header findTokens: $ ) at: 1)) not
|
|
& (self isTaggedAs: 'código') not
|
|
& (self hasAncestorHeaderWith: '%invisible') not
|
|
ifTrue: [
|
|
self headerAsMarkdownInto: markdownStream.
|
|
self bodyAsMarkdownInto: markdownStream ].
|
|
(self headerStartsWith: '%idea')
|
|
ifTrue: [ self bodyAsMarkdownInto: markdownStream ].
|
|
(self headerStartsWith: '%item')
|
|
ifTrue: [ self itemAsMarkdownInto: markdownStream ].
|
|
(self headerStartsWith: '%footnote')
|
|
ifTrue: [ self footnoteAsMarkdownInto: markdownStream ].
|
|
((self isTaggedAs: 'código')
|
|
and: [(self hasAncestorHeaderWith: '%invisible') not
|
|
& (self headerStartsWith: '%embed') not ])
|
|
ifTrue: [ self exportCodeNodeTo: markdownStream ].
|
|
^ markdownStream contents
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> metadata [
|
|
| mnode |
|
|
mnode := self root preorderTraversal
|
|
detect: [ :n | n headerStartsWith: '%metadata' ]
|
|
ifNone: [ ^ nil ].
|
|
^ mnode output.
|
|
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> metadataAsYamlIn: markdownStream [
|
|
"I convert the first '%metadata' node into a YAML preamble contents to be used by Pandoc
|
|
exportation."
|
|
self metadata
|
|
ifNotNil: [
|
|
self metadata
|
|
keysAndValuesDo: [ :k :v |
|
|
k = 'pandocOptions'
|
|
ifTrue: [
|
|
markdownStream
|
|
nextPutAll:
|
|
(k, ': ', self pandocOptionsPrettyYaml) ]
|
|
ifFalse: [
|
|
markdownStream
|
|
nextPutAll:
|
|
(k , ': ' , v asString) withInternetLineEndings;
|
|
lf] ]].
|
|
markdownStream
|
|
nextPutAll: String cr, String cr.
|
|
]
|
|
|
|
{ #category : #movement }
|
|
GrafoscopioNode >> moveAfter [
|
|
"Moves the current node a place before in the children collection where is located"
|
|
| collection index successor |
|
|
collection := self parent children.
|
|
index := collection indexOf: self.
|
|
(index between: 1 and: collection size - 1)
|
|
ifTrue: [
|
|
successor := collection after: self.
|
|
collection at: index + 1 put: self.
|
|
collection at: index put: successor]
|
|
]
|
|
|
|
{ #category : #movement }
|
|
GrafoscopioNode >> moveBefore [
|
|
"Moves the current node a place before in the children collection where is located"
|
|
| collection index predecessor |
|
|
collection := self parent children.
|
|
index := collection indexOf: self.
|
|
(index between: 2 and: collection size)
|
|
ifTrue: [
|
|
predecessor := collection before: self.
|
|
collection at: index -1 put: self.
|
|
collection at: index put: predecessor]
|
|
]
|
|
|
|
{ #category : #'instance creation' }
|
|
GrafoscopioNode >> newNode [
|
|
node := Dictionary newFrom: {
|
|
#header -> 'newHeadline'.
|
|
#body -> ''.
|
|
#children -> #()}.
|
|
^ node.
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> output [
|
|
(self isTaggedAs: 'código') ifFalse: [ ^ self ].
|
|
self body ifNil: [ ^ nil ].
|
|
^ (Compiler evaluate: self body)
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> output: anObject [
|
|
output := anObject
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode >> pandocOptions [
|
|
^ self metadata at: 'pandocOptions' ifAbsent: [ ^ '' ]
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode >> pandocOptionsPrettyYaml [
|
|
"I convert pandoc options, if present into an indented Yaml block."
|
|
| yamlOutput pretyOutput |
|
|
pretyOutput := STON toStringPretty: self pandocOptions.
|
|
yamlOutput := '' writeStream.
|
|
yamlOutput
|
|
nextPutAll:
|
|
'|';
|
|
lf.
|
|
pretyOutput linesDo: [ :line |
|
|
yamlOutput
|
|
nextPutAll:
|
|
' ', line;
|
|
lf ].
|
|
^ yamlOutput contents
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> parent [
|
|
"Returns the parent of the current node"
|
|
^ parent
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> parent: aNode [
|
|
"A parent is a node that has the current node in its children"
|
|
aNode ifNil: [
|
|
parent := aNode.
|
|
^self ].
|
|
aNode parent = self ifTrue: [ ^ self ].
|
|
parent := aNode.
|
|
(aNode children includes: self)
|
|
ifFalse: [ aNode addNode: self ]
|
|
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> pasteFromClipboard [
|
|
| clipchild |
|
|
self class clipboard
|
|
ifNotNil: [
|
|
clipchild := self class clipboard.
|
|
self addNode: clipchild.
|
|
clipchild ]
|
|
ifNil: [ self inform: 'Cache is emtpy. Pleas cut/copy a node before pasting' ]
|
|
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> preorderTraversal [
|
|
nodesInPreorder := OrderedCollection new.
|
|
self visitedGoTo: nodesInPreorder.
|
|
^ nodesInPreorder.
|
|
]
|
|
|
|
{ #category : #movement }
|
|
GrafoscopioNode >> promote [
|
|
"Moves the current node up in the hierachy, making it a slibing of its current parent"
|
|
| collection grandparent |
|
|
collection := self parent children.
|
|
grandparent := self parent parent.
|
|
collection isNotNil & grandparent isNotNil
|
|
ifTrue: [
|
|
(grandparent children) add: self after: (self parent).
|
|
self level: (self parent) level.
|
|
self parent: grandparent.
|
|
collection remove: self.]
|
|
|
|
|
|
]
|
|
|
|
{ #category : #exporting }
|
|
GrafoscopioNode >> publish [
|
|
| publishedUrl |
|
|
(self confirm: 'Publish playground content to the cloud?')
|
|
ifFalse: [ ^ self ].
|
|
self content ifEmpty: [
|
|
self inform: 'Nothing was published because the playground is empty'.
|
|
^ self ].
|
|
Clipboard clipboardText: (publishedUrl := (GTUrlProvider new post: self content) asString).
|
|
self inform: publishedUrl , ' was published and the url was copied to clipboard'
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> removeLastNode [
|
|
"Adds the given node to the receivers collection of children, and sets this object as the parent
|
|
of the node"
|
|
|
|
self children removeLast.
|
|
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> removeNode: aNode [
|
|
(self children includes: aNode)
|
|
ifTrue: [ self children remove: aNode ]
|
|
ifFalse: [ self inform: 'The node doesn''t belong to this node children' ]
|
|
|
|
|
|
]
|
|
|
|
{ #category : #utility }
|
|
GrafoscopioNode >> replaceAccentedHTMLChars [
|
|
self body: (self body copyReplaceAll: 'í' with: 'í' )
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> root [
|
|
"I return the root node of the Grafoscopio tree, i.e the common ancestor of all other nodes"
|
|
self level = 0
|
|
ifFalse: [ ^ self ancestors first ].
|
|
^ self
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> saveContent: anObject [
|
|
"Sets the receivers body to the given object"
|
|
|
|
body := anObject
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> selectMarkdownSubtreesToExport [
|
|
^ (self root preorderTraversal) select: [ :each | each linksToMarkdownFile ].
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> specModelClass [
|
|
|
|
(self isTaggedAs: 'código') ifTrue: [^GrafoscopioCodeModel].
|
|
(self isTaggedAs: 'johan') ifTrue:[^GrafoscopioButtonModel].
|
|
"por defecto"
|
|
^ GrafoscopioTextModel
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> subtreeCopy [
|
|
"I return the same node if its subtree only contains the receiver, or a copy of the receivers
|
|
subtree, in other cases."
|
|
| linearSubtree linearSubtreeCopy |
|
|
linearSubtree := self preorderTraversal.
|
|
linearSubtreeCopy := OrderedCollection new.
|
|
linearSubtree do: [ :cn | linearSubtreeCopy add: cn surfaceCopy ].
|
|
linearSubtreeCopy allButFirst doWithIndex: [ :n :i | | parentPos |
|
|
parentPos := linearSubtree indexOf: (linearSubtree at: i+1) parent.
|
|
n parent: (linearSubtreeCopy at: parentPos) ].
|
|
^ linearSubtreeCopy at: 1.
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> surfaceCopy [
|
|
"I copy the most relevant values of the receiver. I'm useful to avoid copying references
|
|
to the rest of the container tree, which could end in copying the whole tree."
|
|
| newNode |
|
|
newNode := self class new.
|
|
^ newNode
|
|
header: self header;
|
|
body: self body;
|
|
tags: self tags.
|
|
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> tagAs: aTag [
|
|
"Tags the recipient node with aTag (string). For the moment we will have only one tag.
|
|
In the future we will have several and there will be rules to know how tags interact with
|
|
each other"
|
|
aTag = 'código' ifTrue: [ ^ self toggleCodeText ].
|
|
(self tags includes: aTag)
|
|
ifFalse: [ self tags add: aTag ].
|
|
^ self
|
|
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> tags [
|
|
"I returns the receiver tags."
|
|
| migration |
|
|
tags isString
|
|
ifTrue: [
|
|
migration := tags.
|
|
tags := OrderedCollection new.
|
|
self tagAs: migration ].
|
|
tags ifNil: [
|
|
tags := OrderedCollection new.
|
|
self tagAs: 'text' ].
|
|
^ tags
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> tags: aCollection [
|
|
tags := aCollection
|
|
]
|
|
|
|
{ #category : #accessing }
|
|
GrafoscopioNode >> title [
|
|
"Returns the receiver header"
|
|
|
|
^ header
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> toggleCodeText [
|
|
"Some tags are exclusionary.
|
|
For example a node can not be tagged as text and as 'code' (código) simultaneosly.
|
|
In that case, I replace the ocurrence of one tag by the other to warranty that both are not
|
|
in the same node."
|
|
(self isTaggedAs: 'text')
|
|
ifTrue: [ ^ self tags replaceAll: 'text' with: 'código'].
|
|
(self isTaggedAs: 'código')
|
|
ifTrue: [ ^ self tags replaceAll: 'código' with: 'text' ].
|
|
]
|
|
|
|
{ #category : #operation }
|
|
GrafoscopioNode >> visitLastLink [
|
|
self lastLink = ''
|
|
ifTrue: [ self inform: 'This node has no associated links to visit'. ^ self ].
|
|
[WebBrowser openOn: self lastLink] fork
|
|
]
|
|
|
|
{ #category : #'add/remove nodes' }
|
|
GrafoscopioNode >> visitedGoTo: aCollection [
|
|
"Stores the current node in a collection and recursively stores its children"
|
|
|
|
aCollection add: self.
|
|
(self children isNotEmpty) & ((self header findString: '#invisible')=1) not
|
|
ifTrue: [ (self children) do: [ :eachNode | eachNode visitedGoTo: aCollection]].
|
|
|
|
|
|
]
|