124 lines
2.6 KiB
Elm
124 lines
2.6 KiB
Elm
module License exposing
|
|
( assemble
|
|
, compileTemplate
|
|
, DownloadType (..)
|
|
, downloadExt
|
|
, download
|
|
, downloadMime
|
|
, empty
|
|
, License
|
|
, LicenseDescriptors
|
|
, LicenseInfo
|
|
, Template
|
|
, ModuleInfo
|
|
, moduleSorter
|
|
, toggleModule
|
|
)
|
|
|
|
import Html exposing (Html)
|
|
import Sort exposing (Sorter)
|
|
import Sort.Set as Set exposing (Set)
|
|
import File.Download as Download
|
|
|
|
-- Download Types
|
|
|
|
type DownloadType
|
|
= Plain
|
|
| Markdown
|
|
| Html
|
|
|
|
downloadMime : DownloadType -> String
|
|
downloadMime dtype =
|
|
case dtype of
|
|
Plain -> "text/plain"
|
|
Markdown -> "text/markdown"
|
|
Html -> "text/html"
|
|
|
|
downloadExt : DownloadType -> String
|
|
downloadExt dtype =
|
|
case dtype of
|
|
Plain -> "txt"
|
|
Markdown -> "md"
|
|
Html -> "html"
|
|
|
|
|
|
-- Modules
|
|
|
|
type alias ModuleInfo =
|
|
{ name: String
|
|
, desc: List (Html Never)
|
|
, index: Int
|
|
}
|
|
|
|
moduleSorter : (modtype -> ModuleInfo) -> Sorter modtype
|
|
moduleSorter info =
|
|
Sort.by (info >> .index) Sort.increasing
|
|
|
|
|
|
-- Templates
|
|
|
|
type alias Template modtype = List (Maybe modtype, String)
|
|
|
|
compileTemplate : Set modtype -> Template modtype -> String
|
|
compileTemplate modules template =
|
|
template
|
|
|> List.filterMap (checkTemplateFrame modules)
|
|
|> String.join ""
|
|
|
|
checkTemplateFrame : Set modtype -> (Maybe modtype, String) -> Maybe String
|
|
checkTemplateFrame modules (maybemod, content) =
|
|
case maybemod of
|
|
Just mod -> if Set.memberOf modules mod then Just content else Nothing
|
|
Nothing -> Just content
|
|
|
|
|
|
-- Licenses
|
|
|
|
type alias License modtype =
|
|
{ activeModules : Set modtype
|
|
, info : LicenseInfo modtype
|
|
}
|
|
|
|
type alias LicenseInfo modtype =
|
|
{ availableModules : List modtype
|
|
, templates : DownloadType -> Template modtype
|
|
, moduleInfo : modtype -> ModuleInfo
|
|
, descriptors : LicenseDescriptors
|
|
}
|
|
|
|
type alias LicenseDescriptors =
|
|
{ name : String
|
|
, desc : String
|
|
, slug : String
|
|
}
|
|
|
|
empty : LicenseInfo modtype -> License modtype
|
|
empty info =
|
|
License
|
|
(Set.empty (moduleSorter info.moduleInfo))
|
|
info
|
|
|
|
addOrRemove : Bool -> (a -> Set a -> Set a)
|
|
addOrRemove isAdding =
|
|
if isAdding
|
|
then Set.insert
|
|
else Set.remove
|
|
|
|
toggleModule : Bool -> modtype -> License modtype -> License modtype
|
|
toggleModule newState mod license =
|
|
{ license | activeModules = (addOrRemove newState) mod license.activeModules }
|
|
|
|
-- License Assembly
|
|
|
|
assemble : DownloadType -> License mtype -> String
|
|
assemble dtype license =
|
|
license.info.templates dtype
|
|
|> compileTemplate license.activeModules
|
|
|
|
download : DownloadType -> License mtype -> Cmd msg
|
|
download dtype modules =
|
|
Download.string
|
|
("LICENSE." ++ (downloadExt dtype))
|
|
(downloadMime dtype)
|
|
(assemble dtype modules)
|