From cc99feebac40051fb5cf97000fa6402ebb7df316 Mon Sep 17 00:00:00 2001 From: Offray Date: Sun, 10 Dec 2023 12:44:45 -0500 Subject: [PATCH] Moving Pandoc class from GrafoscopioUtils. --- src/MiniDocs/LePictureSnippet.extension.st | 5 - src/MiniDocs/LeTextualSnippet.extension.st | 5 - src/MiniDocs/MiniDocsServer.class.st | 2 +- src/MiniDocs/Pandoc.class.st | 146 +++++++++++++++++++++ src/MiniDocs/String.extension.st | 13 -- 5 files changed, 147 insertions(+), 24 deletions(-) create mode 100644 src/MiniDocs/Pandoc.class.st diff --git a/src/MiniDocs/LePictureSnippet.extension.st b/src/MiniDocs/LePictureSnippet.extension.st index 2664528..4890b4c 100644 --- a/src/MiniDocs/LePictureSnippet.extension.st +++ b/src/MiniDocs/LePictureSnippet.extension.st @@ -45,11 +45,6 @@ LePictureSnippet >> fromString: aString [ ^ self ] -{ #category : #'*MiniDocs' } -LePictureSnippet >> metadata [ - ^ self optionAt: 'metadata' ifAbsentPut: [ self metadataUpdate ] -] - { #category : #'*MiniDocs' } LePictureSnippet >> metadataDiv [ | output | diff --git a/src/MiniDocs/LeTextualSnippet.extension.st b/src/MiniDocs/LeTextualSnippet.extension.st index a38d1b4..5fe9714 100644 --- a/src/MiniDocs/LeTextualSnippet.extension.st +++ b/src/MiniDocs/LeTextualSnippet.extension.st @@ -84,11 +84,6 @@ LeTextualSnippet >> metadataUpdate [ yourself ] -{ #category : #'*MiniDocs' } -LeTextualSnippet >> options [ - ^ options -] - { #category : #'*MiniDocs' } LeTextualSnippet >> sanitizeMetadata [ self options ifNil: [^ self ]. diff --git a/src/MiniDocs/MiniDocsServer.class.st b/src/MiniDocs/MiniDocsServer.class.st index 035e3ae..975e2cf 100644 --- a/src/MiniDocs/MiniDocsServer.class.st +++ b/src/MiniDocs/MiniDocsServer.class.st @@ -20,7 +20,7 @@ MiniDocsServer class >> build [ MiniDocsServer class >> defaultConfiguration [ "Override to set more default values" ^ { - #port -> 9090 + #port -> 1701 } ] diff --git a/src/MiniDocs/Pandoc.class.st b/src/MiniDocs/Pandoc.class.st new file mode 100644 index 0000000..77b667a --- /dev/null +++ b/src/MiniDocs/Pandoc.class.st @@ -0,0 +1,146 @@ +" +I model the interaction between Pandoc and Grafoscopio. +" +Class { + #name : #Pandoc, + #superclass : #Object, + #classInstVars : [ + 'executable' + ], + #category : #'MiniDocs-Core' +} + +{ #category : #'as yet unclassified' } +Pandoc class >> downloadLuaFilters [ + self luaFilters do: [ :filter | | filterUrl | + filterUrl := filter asUrl. + (FileLocator temp asFileReference / (filterUrl segments last)) exists + ifFalse: [ + ZnClient new + url: filterUrl; + downloadTo: FileLocator temp ] ] +] + +{ #category : #accessing } +Pandoc class >> executable [ + ^ executable ifNil: [ self executableLocation ] +] + +{ #category : #accessing } +Pandoc class >> executable: aFileReference [ + executable := aFileReference +] + +{ #category : #accessing } +Pandoc class >> executableLocation [ + | location | + location := '/usr/bin/pandoc'. + location asFileReference exists + ifTrue: [ ^ location ] + ifFalse: [ self definePandocExecutable ] +] + +{ #category : #utility } +Pandoc class >> extractImagesInUnixFor: aFileReference withFilter: aLuaFilter [ + "I use Pandoc Lua scripting capabilities to extract al images links in aFileReference" + + OSSUnixSubprocess new + command: 'pandoc'; + arguments: {aFileReference fullName . '--lua-filter=',aLuaFilter fullName }; + redirectStdout; + redirectStderr; + runAndWaitOnExitDo: [ :process :outString :errString | + process isSuccess + ifTrue: [ + ^ ((Soup fromString: outString) findAllTags: 'td') collect: [ :each | each next ] ] + ifFalse: [ + "OSSUnixProcessExitStatus has a nice #printOn: " + Transcript show: 'Command exit with error status: ', process exitStatusInterpreter printString; cr. + Transcript show: 'Stderr contents: ', errString. + ] + ] +] + +{ #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." + + | filter commandString outputString | + filter := FileLocator temp asFileReference / 'image-links.lua'. + filter exists + ifFalse: [ self downloadLuaFilters ]. + commandString := 'pandoc ' , aFileReference fullName + , ' --lua-filter=' , filter fullName. + ^ self extractImagesInUnixFor: aFileReference withFilter: filter +] + +{ #category : #utility } +Pandoc class >> luaFilters [ + "I define the location of set of scripts, that allows to change the default behaviour of Pandoc + and/or the processing of supported markup languages. + + For more information about Lua filters see: + + https://pandoc.org/lua-filters.html + " + + | filters | + filters := OrderedCollection new. + filters + add: 'http://mutabit.com/repos.fossil/dataweek/doc/tip/Artefactos/Scripts/image-links.lua'. + ^ filters +] + +{ #category : #converters } +Pandoc class >> markdownToHtml: inputFile [ + + (Smalltalk os isUnix or: [ Smalltalk os isMacOS ]) ifTrue: [ ^ self markdownToHtmlOnUnix: inputFile ]. + Smalltalk os isWindows ifTrue: [ ^ self markdownToHtmlOnWindows: inputFile ]. +] + +{ #category : #converters } +Pandoc class >> markdownToHtmlOnUnix: inputFile [ + + | outputFile | + outputFile := FileLocator temp / (inputFile basenameWithoutExtension , '.html'). + outputFile ensureDelete. + outputFile ensureCreateFile. + OSSUnixSubprocess new + command: 'pandoc'; + arguments: {'-f'. 'markdown+startnum+task_lists'. '-t'. 'html'. inputFile fullName. + '--output'. outputFile fullName }; + redirectStdout; + redirectStderr; + runAndWaitOnExitDo: [ :process :outString :errString | + process isSuccess + ifTrue: [ ^ outputFile ] + ifFalse: [ ^ inputFile ] + ] +] + +{ #category : #converters } +Pandoc class >> markdownToHtmlOnWindows: inputFile [ + + "ToDo: This command still doesn't receive any arguments." + ^ (LibC resultOfCommand: 'pandoc ', inputFile fullName) correctAccentedCharacters. +] diff --git a/src/MiniDocs/String.extension.st b/src/MiniDocs/String.extension.st index 2a45029..d536d05 100644 --- a/src/MiniDocs/String.extension.st +++ b/src/MiniDocs/String.extension.st @@ -1,18 +1,5 @@ Extension { #name : #String } -{ #category : #'*MiniDocs' } -String >> accentedCharactersCorrection [ - | modified corrections | - corrections := { - 'ó' -> 'ó' . 'ú' -> 'ú' . 'ñ' -> 'ñ' . - 'í' -> 'í' . 'á' -> 'á' . 'é' -> 'é' . '’' -> $' asString} asDictionary. - modified := self copy. - corrections keysAndValuesDo: [ :k :v | - modified := modified copyReplaceAll: k with: v - ]. - ^ modified -] - { #category : #'*MiniDocs' } String >> asDashedLowercase [ "I convert phrases like 'This is a phrase' into 'this-is-a-phrase'."