Finally, refactoring GrafoscopioNode>>markdownContent to a pretty readable version. May be there is more refactoring to be done, but now one of the key and older algorithms was untangled. :-)

This commit is contained in:
Offray Vladimir Luna Cárdenas 2017-02-16 23:16:18 +00:00
parent 751f60c3df
commit ad07fbe0b9

View File

@ -267,6 +267,13 @@ GrafoscopioNode >> bodyAsCode [
^ codeBody contents ^ 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 withInternetLineEndings); crlf; crlf].
]
{ #category : #accessing } { #category : #accessing }
GrafoscopioNode >> children [ GrafoscopioNode >> children [
"Returns the receivers list of children" "Returns the receivers list of children"
@ -427,6 +434,16 @@ GrafoscopioNode >> flatten [
] ]
] ]
{ #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.
aStream nextPutAll: (self body contents withInternetLineEndings); lf; lf.
]
{ #category : #exporting } { #category : #exporting }
GrafoscopioNode >> hasAncestorHeaderWith: aSpecialWord [ GrafoscopioNode >> hasAncestorHeaderWith: aSpecialWord [
"Looks if the receptor node has an ancestor with a header with 'aSpecialWord' as the only or the first word" "Looks if the receptor node has an ancestor with a header with 'aSpecialWord' as the only or the first word"
@ -467,6 +484,15 @@ GrafoscopioNode >> header: anObject [
] ]
{ #category : #exporting } { #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 [ GrafoscopioNode >> headerStartsWith: aString [
^ (self header findString: aString) = 1 ^ (self header findString: aString) = 1
] ]
@ -576,44 +602,23 @@ GrafoscopioNode >> margins [
{ #category : #exporting } { #category : #exporting }
GrafoscopioNode >> markdownContent [ GrafoscopioNode >> markdownContent [
"Extracts the markdown of a node using body as content, header as title and level as hierarchical level of the title. "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, converts them into proper markup to be embedded inside markdown" If special nodes types are present, that use %keywords in its header or body I convert them into proper markup"
| markdown temporalBody | | markdownStream |
markdown := '' writeStream. markdownStream := '' writeStream.
(self class specialWords includes: self header) not & (self class specialWords includes: self header) not &
(self class specialWords includes: ((self header findTokens: $ ) at: 1)) not & (self tags ~= 'código') & (self class specialWords includes: ((self header findTokens: $ ) at: 1)) not & (self tags ~= 'código') &
(self hasAncestorHeaderWith: '%invisible') not (self hasAncestorHeaderWith: '%invisible') not
ifTrue: [ ifTrue: [
self level timesRepeat: [ markdown nextPutAll: '#' ]. self headerAsMarkdownInto: markdownStream.
markdown nextPutAll: ' '. self bodyAsMarkdownInto: markdownStream].
markdown nextPutAll: (self header copyReplaceTokens: #cr with: #lf); crlf; crlf.
temporalBody := self body asString.
self embeddedNodes ifNotNil: [
(temporalBody includesSubstring: '%embed-all')
ifFalse: [temporalBody := self embedNodes ]
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: '']].
markdown nextPutAll: (temporalBody contents withInternetLineEndings ); crlf; crlf].
(self headerStartsWith: '%idea') (self headerStartsWith: '%idea')
ifTrue: [ ifTrue: [self bodyAsMarkdownInto: markdownStream].
temporalBody := self body string.
self embeddedNodes ifNotNil: [
self embeddedNodes do: [ :each |
each tags = 'código'
ifFalse: [temporalBody := temporalBody copyReplaceAll: (each header) with: each body]
ifTrue: [temporalBody := temporalBody copyReplaceAll: (each header) with: each bodyAsCode]]].
markdown nextPutAll: (temporalBody contents withUnixLineEndings); lf; lf.
].
(self headerStartsWith: '%footnote') (self headerStartsWith: '%footnote')
ifTrue: [ ifTrue: [ self footnoteAsMarkdownInto: markdownStream ].
markdown nextPutAll: ('[^',(self header copyReplaceAll: '%footnote ' with: ''),']: ' ); lf.
markdown nextPutAll: (self body contents withInternetLineEndings); lf; lf. ].
((self tags = 'código') and: [(self hasAncestorHeaderWith: '%invisible') not & (self headerStartsWith: '%embed') not ]) ((self tags = 'código') and: [(self hasAncestorHeaderWith: '%invisible') not & (self headerStartsWith: '%embed') not ])
ifTrue: [ self exportCodeBlockTo: markdown ]. ifTrue: [ self exportCodeBlockTo: markdownStream ].
^ markdown contents ^ markdownStream contents
] ]
{ #category : #accessing } { #category : #accessing }