Skip to content

Added export PDF functionality #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/common/HaskellDo/Compilation/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ buildOutput state = do
System.ExitSuccess ->
return state { compiledOutput = preprocessOutput out, compilationError = "", dirtyCompile = True }


preprocessOutput :: String -> String
preprocessOutput out =
Text.pack out
Expand Down
1 change: 0 additions & 1 deletion src/common/HaskellDo/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,3 @@ handleRead :: Either SomeException String -> IO (Maybe String)
handleRead = \case
Left _ -> return Nothing
Right txt -> return (Just txt)

21 changes: 21 additions & 0 deletions src/common/HaskellDo/Toolbar/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module HaskellDo.Toolbar.State where

import System.Directory (listDirectory, doesFileExist, doesDirectoryExist, getHomeDirectory, createDirectory)
import System.FilePath ((</>))
import System.Process (callCommand, shell,readCreateProcessWithExitCode)
--import Data.List (isInfixOf)
import System.Exit

import Control.Monad (filterM, unless)

Expand Down Expand Up @@ -134,7 +137,25 @@ update ToggleError state = do
localIO toggleError
return state

update ConvertToPDF state = do
(errorCode,_,_) <- atRemote . localIO $ readCreateProcessWithExitCode (shell "which wkhtmltopdf") ""
let environmentVar = checkError errorCode :: Bool
let path = projectPath state
if (environmentVar == True) && ((projectOpened state) == True)
then do
localIO $ openModal "#convertToPDFModal"
atRemote . localIO $ callCommand ("cd " ++ path ++ " && stack exec run-test > index.html && wkhtmltopdf index.html index.pdf" :: String)
else
localIO $ openModal "#convertToPDFModalFail"
return state

update _ state = return state

checkError :: ExitCode -> Bool
checkError exitCode =
case exitCode of
ExitSuccess -> True
ExitFailure _ -> False

shakeErrorDisplay :: IO ()
shakeErrorDisplay = shake "#errorDisplay"
1 change: 1 addition & 0 deletions src/common/HaskellDo/Toolbar/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ data Action
| ClosePackageModal
| ToggleEditor
| ToggleError
| ConvertToPDF
deriving (Read, Show)
22 changes: 21 additions & 1 deletion src/common/HaskellDo/Toolbar/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ toolbar = rawHtml $ do
li ! id "packageEditorButton" $ noHtml
li ! id "toggleEditorButton" $ noHtml
li ! id "toggleErrorButton" $ noHtml
li ! id "convertToPDFButton" $ noHtml
packageEditorModal -- Apparently, if we put this line
openProjectModal -- under this one. The open project modal doesn't work
modalPromptPlaceholder "newDirectoryModal" "New Directory" "Choose a name for the new directory"
convertToPDFModal
convertToPDFModalFail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're rendering the modals inside of the toolbar. If there's no reason for that, I'd suggest to render them in the body element instead, final result won't differ, but I think that makes more sense 😜

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I though there was the place where modals belong, maybe my mistake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's a mistake by my part, instead of making a separate component for each modal I just jammed em all into the toolbar, which is awful, see #76


openProjectModal :: Perch
openProjectModal =
Expand All @@ -62,8 +65,20 @@ openProjectModal =
div ! atr "class" "modal-footer" $
div ! id "closeModalButton" $ noHtml

convertToPDFModal :: Perch
convertToPDFModal =
div ! id "convertToPDFModal" ! atr "class" "modal" $ do
div ! atr "class" "modal-content" $ do
h4 ("PDF saved on project path" :: String)

convertToPDFModalFail :: Perch
convertToPDFModalFail =
div ! id "convertToPDFModalFail" ! atr "class" "modal" $ do
div ! atr "class" "modal-content red darken-1 white-text" $ do
h4 ("Error: wkhtmltopdf is not installed or a project has not been loaded" :: String)

modalPromptPlaceholder :: String -> String -> String -> Perch
modalPromptPlaceholder id' htitle text =
modalPromptPlaceholder id' htitle text =
div ! id id' ! atr "class" "modal" $ do
div ! atr "class" "modal-content" $ do
if (not . null) htitle then h4 htitle else noHtml
Expand Down Expand Up @@ -125,6 +140,11 @@ toggleErrorButton _ = Ulmus.newWidget "toggleErrorButton" $ wlink ToggleError $
a ! atr "class" "btn-floating purple darken-2 tooltipped" ! atr "data-position" "bottom" ! atr "data-tooltip" "Toggle error" ! atr "data-delay" "50"$
i ! atr "class" "material-icons" $ ("error" :: String)

convertToPDFButton :: State -> Widget Action
convertToPDFButton _ = Ulmus.newWidget "convertToPDFButton" $ wlink ConvertToPDF $
a ! atr "class" "btn-floating purple darken-2 tooltipped" ! atr "data-position" "bottom" ! atr "data-tooltip" "Convert to PDF" ! atr "data-delay" "50"$
i ! atr "class" "material-icons" $ ("picture_as_pdf" :: String)


closeModalButton :: State -> Widget Action
closeModalButton _ = Ulmus.newWidget "closeModalButton" $ wlink LoadProject $
Expand Down
4 changes: 4 additions & 0 deletions src/common/HaskellDo/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ widgets state = do
**> packageEditorButtonWidget
**> toggleEditorButtonWidget
**> toggleErrorButtonWidget
**> convertToPDFButtonWidget
**> compileButtonWidget
**> pathInputWidget
**> closeModalButtonWidget
Expand Down Expand Up @@ -98,6 +99,9 @@ widgets state = do
toggleErrorButtonWidget = Ulmus.mapAction ToolbarAction $
Toolbar.toggleErrorButton (toolbarState state)

convertToPDFButtonWidget = Ulmus.mapAction ToolbarAction $
Toolbar.convertToPDFButton (toolbarState state)

pathInputWidget = Ulmus.mapAction ToolbarAction $
Toolbar.pathInput (toolbarState state)

Expand Down