From fe3d9ff84c494cb14b19336655f5495740fce11f Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Tue, 4 Dec 2018 23:53:05 +0000 Subject: [PATCH] Debugging Pandoc HTML to Markdown importation, now using OSSubprocess. --- .../Grafoscopio/GrafoscopioNode.class.st | 17 ++++------------ .../Grafoscopio/GrafoscopioNotebook.class.st | 18 +++++++++++++++++ repository/Grafoscopio/Pandoc.class.st | 20 +++++++++++++++++++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/repository/Grafoscopio/GrafoscopioNode.class.st b/repository/Grafoscopio/GrafoscopioNode.class.st index dd87b51..7436afa 100644 --- a/repository/Grafoscopio/GrafoscopioNode.class.st +++ b/repository/Grafoscopio/GrafoscopioNode.class.st @@ -522,27 +522,18 @@ GrafoscopioNode >> headers [ { #category : #operation } GrafoscopioNode >> htmlToMarkdown [ "I convert the node body from HTML format to Pandoc's Markdown." - | htmlFile markdownFile | - + | htmlFile | (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 ensureCreateFile. 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 ]. + ifTrue: [ self body: (Pandoc htmlToMarkdown: htmlFile) ]. Smalltalk platformName = 'Win32' - ifTrue: [ WinProcess - createAndWaitForProcess: 'pandoc -f html -t markdown --atx-headers ', - htmlFile fullName, ' -o ', markdownFile fullName ]. - self body: markdownFile contents. + ifTrue: [ self shouldBeImplemented ]. htmlFile ensureDelete. - markdownFile ensureDelete. ] { #category : #operation } diff --git a/repository/Grafoscopio/GrafoscopioNotebook.class.st b/repository/Grafoscopio/GrafoscopioNotebook.class.st index f02783b..d839db6 100644 --- a/repository/Grafoscopio/GrafoscopioNotebook.class.st +++ b/repository/Grafoscopio/GrafoscopioNotebook.class.st @@ -170,6 +170,24 @@ GrafoscopioNotebook >> demoteNode [ self notebookContent: notebook. ] +{ #category : #'as yet unclassified' } +GrafoscopioNotebook >> downloadImages [ + "I download all images in a notebook into a local folder that respects relative paths. + So if a image refers to http://mysite.com/uploads/chap1/myimage.png, it will be stored + into: 'uploads/chap1/myimage.png' in the same folder where the notebook is stored. + This is helpful for notebooks conversions that expect to have local images in particular + locations." + + | parentFolder | + parentFolder := self workingFile parent. + self. + ^ self imagesList do: [ :each | | relativePathString link | + link := each contents asUrl. + relativePathString := link directory. + relativePathString ifNotEmpty: [ + GrafoscopioUtils ensureCreateDirectory: relativePathString into: parentFolder ]] +] + { #category : #persistence } GrafoscopioNotebook >> exportAllSubtreesAsMarkdow [ | toBeExported | diff --git a/repository/Grafoscopio/Pandoc.class.st b/repository/Grafoscopio/Pandoc.class.st index 4a174f7..67ce328 100644 --- a/repository/Grafoscopio/Pandoc.class.st +++ b/repository/Grafoscopio/Pandoc.class.st @@ -61,6 +61,26 @@ Pandoc class >> extractImagesInUnixFor: aFileReference withFilter: aLuaFilter [ ] ] +{ #category : #converters } +Pandoc class >> htmlToMarkdown: inputFile [ + + | outputFile | + outputFile := FileLocator temp / 'body.md'. + outputFile ensureDelete. + outputFile ensureCreateFile. + OSSUnixSubprocess new + command: 'pandoc'; + arguments: {'-f'. 'html'. '-t'. 'markdown'. '--atx-headers'. inputFile fullName. + '--output'. outputFile fullName }; + redirectStdout; + redirectStderr; + runAndWaitOnExitDo: [ :process :outString :errString | + process isSuccess + ifTrue: [ ^ outputFile contents ] + ifFalse: [ ^inputFile contents ] + ] +] + { #category : #'as yet unclassified' } Pandoc class >> listImagesFrom: aFileReference [ "I provide a list of all images contained in aFile."