Skip to content
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

mdBook Assets - Generated Makefile Approach #3847

Closed
wants to merge 8 commits into from
Closed
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
8 changes: 4 additions & 4 deletions code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ $(filter %$(TEX_E_SUFFIX), $(TEX_EXAMPLES)): %$(TEX_E_SUFFIX): %$(TRACE_GRAPH_SU

# Build individual example mdBook SRSs.
# Targets will have the form exampleName_mdBook_build.
$(filter %$(MDBOOK_BUILD_E_SUFFIX), $(MDBOOK_BUILD_EXAMPLES)): %$(MDBOOK_BUILD_E_SUFFIX): $(CLEAN_GF_PREFIX)$(LOG_FOLDER_NAME) %$(GEN_E_SUFFIX)
mdbook build "$(BUILD_FOLDER)$(EDIR)/SRS/mdBook"
$(filter %$(MDBOOK_BUILD_E_SUFFIX), $(MDBOOK_BUILD_EXAMPLES)): %$(MDBOOK_BUILD_E_SUFFIX): $(CLEAN_GF_PREFIX)$(LOG_FOLDER_NAME) %$(GEN_E_SUFFIX) %$(TRACE_GRAPH_SUFFIX)
cd "$(BUILD_FOLDER)$(EDIR)/SRS/mdBook" && $(MAKE) build

# Launch local server for individual example mdBook SRSs.
# Targets will have the form exampleName_mdBook_server.
$(filter %$(MDBOOK_SERVER_E_SUFFIX), $(MDBOOK_SERVER_EXAMPLES)): %$(MDBOOK_SERVER_E_SUFFIX): $(CLEAN_GF_PREFIX)$(LOG_FOLDER_NAME) %$(GEN_E_SUFFIX)
mdbook serve --open "$(BUILD_FOLDER)$(EDIR)/SRS/mdBook"
$(filter %$(MDBOOK_SERVER_E_SUFFIX), $(MDBOOK_SERVER_EXAMPLES)): %$(MDBOOK_SERVER_E_SUFFIX): $(CLEAN_GF_PREFIX)$(LOG_FOLDER_NAME) %$(GEN_E_SUFFIX) %$(TRACE_GRAPH_SUFFIX)
cd "$(BUILD_FOLDER)$(EDIR)/SRS/mdBook" && $(MAKE) server

#----------------------------------------------------------------------------------#
#--- Generate analysis of module, class, datatype, and class instance structure ---#
Expand Down
18 changes: 12 additions & 6 deletions code/drasil-gen/lib/Language/Drasil/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import Language.Drasil.Printers (DocType(SRS, Website, Lesson), makeCSS, genHTML
import Language.Drasil.Code (generator, generateCode, Choices(..), CodeSpec(..),
Lang(..), getSampleData, readWithDataDesc, sampleInputDD,
unPP, unJP, unCSP, unCPPP, unSP)
import Language.Drasil.Output.Formats(Filename, DocSpec(DocSpec), DocChoices(DC))
import Language.Drasil.Output.Formats(Filename, DocSpec(DocSpec), DocChoices(DC),
MakeSpec(MakeSpec))

import Language.Drasil.TypeCheck
import Language.Drasil.Dump
Expand All @@ -58,11 +59,12 @@ prntDoc d pinfo fn dtype fmt =
HTML -> do prntDoc' dtype (show dtype ++ "/HTML") fn HTML d pinfo
prntCSS dtype fn d
TeX -> do prntDoc' dtype (show dtype ++ "/PDF") fn TeX d pinfo
prntMake $ DocSpec (DC dtype []) fn
prntMake $ MakeSpec dtype TeX fn pinfo
Jupyter -> do prntDoc' dtype (show dtype ++ "/Jupyter") fn Jupyter d pinfo
(Markdown GitHub) -> do prntDoc' dtype (show dtype ++ "/Markdown") fn (Markdown GitHub) d pinfo
MDBook -> do prntDoc' dtype (show dtype ++ "/mdBook") fn MDBook d pinfo
prntBook dtype d pinfo
prntMake $ MakeSpec dtype MDBook fn pinfo
_ -> mempty

-- | Helper function to produce an error when an incorrect SRS format is used.
Expand Down Expand Up @@ -97,11 +99,15 @@ prntDoc' dt dt' fn format body' sm = do
getExt _ = srsFormatError

-- | Helper for writing the Makefile(s).
prntMake :: DocSpec -> IO ()
prntMake ds@(DocSpec (DC dt _) _) =
do outh <- openFile (show dt ++ "/PDF/Makefile") WriteMode
hPutStrLn outh $ render $ genMake [ds]
prntMake :: MakeSpec -> IO ()
prntMake ms@(MakeSpec dt f _ _) =
do outh <- openFile (show dt ++ dir f ++ "/Makefile") WriteMode
hPutStrLn outh $ render $ genMake [ms]
hClose outh
where
dir TeX = "/PDF"
dir MDBook = "/mdBook"
dir _ = error "Makefile(s) only supported for TeX/MDBook."

-- | Helper that creates a CSS file to accompany an HTML file.
-- Takes in the folder name, generated file name, and the document.
Expand Down
30 changes: 25 additions & 5 deletions code/drasil-gen/lib/Language/Drasil/Output/Formats.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
module Language.Drasil.Output.Formats where

import Data.Char (toLower)
import System.FilePath (takeDirectory)
import Build.Drasil ((+:+), Command, makeS, mkCheckedCommand, mkCommand, mkFreeVar,
mkFile, mkRule, RuleTransformer(makeRule))
import Language.Drasil.Printers (DocType(..), Format)
import Language.Drasil.Printers (DocType(..), Format(TeX, MDBook),
PrintingInformation, assetMap)

-- | When choosing your document, you must specify the filename for
-- the generated output (specified /without/ a file extension).
Expand All @@ -19,17 +21,35 @@ data DocChoices = DC {
-- | Document specifications. Holds the type of document ('DocType') and its name ('Filename').
data DocSpec = DocSpec DocChoices Filename

-- | Allows the creation of Makefiles for documents that use LaTeX.
instance RuleTransformer DocSpec where
makeRule (DocSpec (DC Website _) _) = []
makeRule (DocSpec (DC dt _) fn) = [
-- | Makefile specification.
-- Holds the type of document ('DocType'), document format ('Format'),
-- its name ('Filename'), and printing information ('PrintingInformation')
data MakeSpec = MakeSpec DocType Format Filename PrintingInformation

-- | Allows the creation of Makefiles for documents that use LaTeX and/or mdBook.
instance RuleTransformer MakeSpec where
-- Makefile for TeX to compile the .tex files to PDF.
makeRule (MakeSpec dt TeX fn _) = [
mkRule [] (makeS $ map toLower $ show dt) [pdfName] [],
mkFile [] pdfName [makeS $ fn ++ ".tex"] $
map ($ fn) [lualatex, bibtex, lualatex, lualatex]] where
lualatex, bibtex :: String -> Command
lualatex = mkCheckedCommand . (+:+) (makeS "lualatex" +:+ mkFreeVar "TEXFLAGS") . makeS
bibtex = mkCommand . (+:+) (makeS "bibtex" +:+ mkFreeVar "BIBTEXFLAGS") . makeS
pdfName = makeS $ fn ++ ".pdf"
-- Makefile for MDBook to build book, launch server, and copy assets.
makeRule (MakeSpec _ MDBook _ sm) = [
mkRule [] (makeS "build") [assets] [build],
mkRule [] (makeS "server") [assets] [server],
mkRule [] (assets) [] (map cmd (assetMap sm))]
where
build = mkCheckedCommand $ makeS "mdbook build"
server = mkCheckedCommand $ makeS "mdbook serve --open"
assets = makeS "assets"
cmd st = mkCommand $ makeS $ mkdir st ++ " && " ++ copy st
copy (s, t) = "cp \"" ++ s ++ "\" \"" ++ t ++ "\""
mkdir (_, t) = "mkdir -p \"" ++ takeDirectory t ++ "\""
makeRule _ = []

-- | LaTeX helper.
data DocClass = DocClass (Maybe String) String
Expand Down
1 change: 1 addition & 0 deletions code/drasil-gen/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- bytestring
- pretty
- containers
- filepath
- directory
- time
- drasil-build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
-- | Defines functions to create .toml config files for mdBook.
module Language.Drasil.Markdown.Book where
-- | Defines functions to create .toml and .csv config files for mdBook.
module Language.Drasil.Markdown.Config where

import Text.PrettyPrint (Doc, text, vcat, (<+>))
import Data.Map (empty)
import Data.Map (empty, elems)
import Control.Lens

import Language.Drasil.Markdown.Print (pSpec)
import Language.Drasil.Printing.PrintingInformation (PrintingInformation)
import Language.Drasil.Markdown.Helpers (extractFn)
import Language.Drasil.Printing.PrintingInformation (PrintingInformation(..))
import Database.Drasil (labelledcontentTable)
import Language.Drasil.Printing.Import.Sentence (spec)
import Language.Drasil.Printing.LayoutObj (Filepath)

import Language.Drasil hiding (Expr)

Expand All @@ -24,7 +28,19 @@ makeBook (Document t _ _ _) sm = vcat [
]
makeBook _ _ = error "Type not supported: Notebook."

-- | Helper function to render the title
-- 'Sentence' as a 'Doc'
mkTitle :: PrintingInformation -> Sentence -> Doc
mkTitle sm t = text "\"" <> pSpec empty ts <> text "\""
where
ts = spec sm t

-- | Helper function to map the original filepaths of assets
-- to the location mdBook uses.
assetMap :: PrintingInformation -> [(Filepath, FilePath)]
assetMap (PI {_ckdb = cdb}) =
[(fp, "src/assets/" ++ extractFn fp)
| (LblC { _ctype = Figure _ fp _ }, _) <- elems lct
]
where
lct = cdb ^. labelledcontentTable
10 changes: 8 additions & 2 deletions code/drasil-printers/lib/Language/Drasil/Markdown/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Text.PrettyPrint (Doc, text, empty, (<>), (<+>), hcat,
brackets, parens, braces)
import Data.Map (lookup)
import Language.Drasil.Printing.Helpers (ast, ($^$), vsep)
import Language.Drasil.Printing.LayoutObj (RefMap)
import Language.Drasil.Printing.LayoutObj (RefMap, Filepath)

data Variation = Id | Align | None

Expand Down Expand Up @@ -78,7 +78,9 @@ reflinkURI ref txt = if ref == txt then ang ref

-- | Helper for setting up figures
image :: Doc -> Doc -> Doc
image f c = text "!" <> reflinkURI f c $^$ bold (caption c)
image f c = text "!" <> reflinkURI fp c $^$ bold (caption c)
where
fp = text $ "/assets/" ++ (extractFn $ show f)

-- | Helper for setting up captions
caption :: Doc -> Doc
Expand Down Expand Up @@ -107,3 +109,7 @@ h' n
-- | Helper for getting length of a Doc
docLength :: Doc -> Int
docLength d = length $ show d

-- | Helper for extracting the file from a filepath
extractFn :: Filepath -> Filepath
extractFn = reverse . takeWhile (/= '/') . reverse
2 changes: 1 addition & 1 deletion code/drasil-printers/lib/Language/Drasil/Markdown/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ printLO rm (EqnBlock contents) = text "\\\\[" <> rndr contents <> text "\\\\]"
printLO rm (Table _ rows r b t) = makeTable rm rows (pSpec rm r) b (pSpec rm t)
printLO rm (Definition _ ssPs l) = makeDefn rm ssPs (pSpec rm l)
printLO rm (List t) = makeList rm t 0
printLO rm (Figure r c f _) = makeFigure (pSpec rm r) (pSpec rm c) (text $ "../" ++ f)
printLO rm (Figure r c f _) = makeFigure (pSpec rm r) (pSpec rm c) (text f)
printLO rm (Bib bib) = makeBib rm bib
printLO _ Graph {} = empty
printLO _ CodeBlock {} = empty
Expand Down
4 changes: 2 additions & 2 deletions code/drasil-printers/lib/Language/Drasil/Printers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Language.Drasil.Printers (
-- * Jupyter
, genJupyter
-- * Markdown
, genMD, genMDBook, makeBook
, genMD, genMDBook, makeBook, assetMap
-- * Log
, printAllDebugInfo
-- * Printing Information and Options
Expand All @@ -46,7 +46,7 @@ import Language.Drasil.HTML.CSS (makeCSS)
import Language.Drasil.HTML.Print (genHTML)
import Language.Drasil.JSON.Print (genJupyter)
import Language.Drasil.Markdown.Print (genMD, genMDBook)
import Language.Drasil.Markdown.Book (makeBook)
import Language.Drasil.Markdown.Config (makeBook, assetMap)
import Language.Drasil.Markdown.CreateMd (makeMd, introInfo, verInfo, unsupOS,
extLibSec, instDoc, regularSec, endNote, whatInfo)
import Language.Drasil.Plain.Print (SingleLine(..), sentenceDoc, exprDoc,
Expand Down
14 changes: 7 additions & 7 deletions code/stable/dblpend/SRS/Markdown/DblPend_SRS.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions code/stable/dblpend/SRS/mdBook/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/stable/dblpend/SRS/mdBook/src/SecPhysSyst.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/stable/dblpend/SRS/mdBook/src/SecSysContext.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions code/stable/dblpend/SRS/mdBook/src/SecTraceMatrices.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions code/stable/gamephysics/SRS/Markdown/GamePhysics_SRS.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading