Skip to content

Commit

Permalink
Updating website to give links to different code languages
Browse files Browse the repository at this point in the history
  • Loading branch information
danscime committed Jun 26, 2019
1 parent 5b6e0c1 commit d77b0ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
12 changes: 7 additions & 5 deletions code/website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ <h3 id="caseStudies">Case Studies</h3>
<li>$name$ SRS
$-for(srs)$
<a href="$url$">[$type$]</a>
$endfor$
$-endfor$
</li>
$-for(src)$
$-if(src)$
<li>
Generated Code
<a href="$path-$">LINKKKK</a>
Generated Code:
$-for(src)$
<a href="$path$">[$lang$]</a>
$-endfor$
</li>
$-endfor$
$-endif$
</ul>
<br>
</li>$sep-$
Expand Down
45 changes: 16 additions & 29 deletions code/website/site.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import System.FilePath (takeBaseName, takeExtension)

-- type FilePath = String -- from System.FilePath
type Name = String
type SourceLocation = [String]
type CodeSource = (FilePath, String)
type SRSVariants = [(Name, String)]
type Description = Maybe String
data Example = E Name SourceLocation SRSVariants Description
data Example = E Name [CodeSource] SRSVariants Description

-- Returns FilePath of the SRS
srsPath :: FilePath -> FilePath -> FilePath -> FilePath
Expand All @@ -33,14 +33,25 @@ getExtensionName _ = error "Expected some extension."
getSRS :: [Name] -> SRSVariants
getSRS = map (\x -> (x, getExtensionName $ takeExtension x)) . filter (\x -> any (`endswith` x) [".pdf", ".html"])

getSrc :: String -> FilePath -> CodeSource
getSrc repoRoot source = (repoRoot ++ source, lang $ takeBaseName source)
where
-- Some languages might be named differently than the folders they are stored in.
lang "cpp" = "C++"
lang "csharp" = "C#"
lang "python" = "Python"
lang "java" = "Java"
lang x = error ("No given display name for language: " ++ x)

mkExamples :: String -> FilePath -> FilePath -> IO [Example]
mkExamples repoRoot path srsDir = do
-- names will be a list of example names (Chipmunk, GlassBR, etc. of type FilePath)
names <- sort <$> (listDirectory path >>= filterM (\x -> doesDirectoryExist $ path ++ x))

-- a list of Just sources or Nothing based on existence and contents of src file.
sources <- mapM (\x -> doesFileExist (path ++ x ++ "/src") >>=
\y -> if y then map ((++) repoRoot) . lines . rstrip <$> readFile (path ++ x ++ "/src") else return []) names
\y -> if y then map (getSrc repoRoot) . lines . rstrip <$> readFile (path ++ x ++ "/src") else return []) names


-- a list of Just descriptions or Nothing based on existence and contents of desc file.
descriptions <- mapM (\x -> doesFileExist ("descriptions/" ++ x ++ ".txt") >>=
Expand All @@ -62,28 +73,6 @@ maybeField s f = Context $ \k _ i -> do
else
fail $ "maybeField " ++ s ++ " used when really Nothing. Wrap in `$if(" ++ s ++ ")$` block."

----------- New additions ---------
-- Could be better optimized

maybeListField :: String -> (Item a -> Compiler (Maybe [String])) -> Int -> Context a
maybeListField s f n = Context $ \k _ i -> do
val <- f i
if k == s && isJust val then
return $ StringField $ (fromJust val)!!n
else
fail $ "maybeField " ++ s ++ " used when really Nothing. Wrap in `$if(" ++ s ++ ")$` block."

takeFromSlash :: String -> String
takeFromSlash s = takeFromSlashHelper s ""
where
takeFromSlashHelper begin end
| last begin == '/' = end
| otherwise = takeFromSlashHelper (init begin) (last begin :end)


-- maybeListField "src" (return . src . itemBody) 0 <>
-- maybeListField takeFromSlash (return . src . itemBody)

mkExampleCtx :: FilePath -> FilePath -> Context Example
mkExampleCtx exampleDir srsDir =
listFieldWith "srs" (
Expand All @@ -95,11 +84,9 @@ mkExampleCtx exampleDir srsDir =
field "name" (return . name . itemBody) <>
maybeField "desc" (return . desc . itemBody) <>
listFieldWith "src" (
-- need to be indexed
field "path" (return . fst . itemBody)
-- field "lang" (function2)
field "path" (return . fst . fst . itemBody) <>
field "lang" (return . snd . fst . itemBody)
) (\x -> mapM (\y -> makeItem (y, itemBody x)) $ src $ itemBody x)

This comment has been minimized.

Copy link
@Mornix

Mornix Jun 26, 2019

Collaborator

If you check if src is empty here and fail if it is, that should allow the templated $if(src)$ to work as expected.

This comment has been minimized.

Copy link
@danscime

danscime Jun 27, 2019

Author Collaborator

Not entirely sure how to implement there specifically.
I think I could make do it easily via the maybeListField idea

This comment has been minimized.

Copy link
@danscime

danscime Jun 27, 2019

Author Collaborator

Edit, my idea for maybeListField also has some issues that I can't seem to figure out. @Mornix how easy would your way be?

This comment has been minimized.

Copy link
@Mornix

Mornix Jun 27, 2019

Collaborator

maybeListField could be a good idea.

This comment has been minimized.

Copy link
@danscime

danscime Jun 27, 2019

Author Collaborator

Still can't seem to figure it out yet.

This is what I'm trying, but I can't figure out how to retain my functions within the creation of Context.

Screen Shot 2019-06-27 at 3 25 09 PM

-- maybeListField "src" (return . src . itemBody) 0
where
name (E nm _ _ _) = nm
src (E _ s _ _) = s
Expand Down

0 comments on commit d77b0ec

Please sign in to comment.