Browse Source

First commit

master
scratchyone 2 months ago
commit
2171dd4b78
No known key found for this signature in database GPG Key ID: 6D7D2C6860A90C02
15 changed files with 515 additions and 0 deletions
  1. +4
    -0
      .gitignore
  2. +3
    -0
      ChangeLog.md
  3. +30
    -0
      LICENSE
  4. +22
    -0
      README.md
  5. +2
    -0
      Setup.hs
  6. +163
    -0
      app/Main.hs
  7. +5
    -0
      download_manpages.sh
  8. +73
    -0
      man.cabal
  9. +52
    -0
      package.yaml
  10. +68
    -0
      stack.yaml
  11. +27
    -0
      stack.yaml.lock
  12. BIN
      static/gay.png
  13. +46
    -0
      static/style.css
  14. +18
    -0
      template.html
  15. +2
    -0
      test/Spec.hs

+ 4
- 0
.gitignore View File

@ -0,0 +1,4 @@
.stack-work/
*~
manpages
out

+ 3
- 0
ChangeLog.md View File

@ -0,0 +1,3 @@
# Changelog for man
## Unreleased changes

+ 30
- 0
LICENSE View File

@ -0,0 +1,30 @@
Copyright Author name here (c) 2021
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Author name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 22
- 0
README.md View File

@ -0,0 +1,22 @@
# Man Page Viewer
Simple man page viewer for [unix.lgbt](https://unix.lgbt).
## Setup
```sh
./download_manpages.sh
```
## Usage
```sh
stack run
# Resulting files will be in ./out/
```
To modify the HTML documents that are generated, you can edit template.html or modify the files in the static folder.
## Limitations
The current parser fails to properly handle the markup example used in `forum(9)`, I suggest you modify or remove it after downloading the source manfiles.

+ 2
- 0
Setup.hs View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

+ 163
- 0
app/Main.hs View File

@ -0,0 +1,163 @@
module Main where
import Data.Functor.Identity (Identity)
import Data.String.Utils
import Shelly (cp_r, shelly)
import System.Directory (canonicalizePath, createDirectory, listDirectory, removePathForcibly)
import System.FilePath.Posix
import Text.Parsec.Prim (ParsecT, Stream)
import Text.ParserCombinators.Parsec
data ManGroup = Header String | Title String | Paragraph [InlineBlock] | List [InlineBlock]
deriving (Show)
data InlineBlock = Text String | Code String | Link String Int
deriving (Show)
manpage :: Parser [ManGroup]
manpage = do
groups <- many (wss manGroup)
eof
return groups
manGroup = try header <|> try title <|> try list <|> Paragraph <$> paragraph
genericBigText :: Char -> Parser String
genericBigText d = do
text <- many1Till anyChar (char '\n')
count (length text) (char d)
return text
header = Header <$> genericBigText '-'
title =
Title
<$> ( do
text <- lookAhead $ many1Till anyChar (char '\n')
name <- many1 letter
char ' '
version <- many1 digit
many1Till anyChar (char '\n')
count (length text - 5) (char '=')
manyTill (char '=') (char '\n')
return $ name ++ "(" ++ version ++ ")"
)
paragraph =
many1Till
(code <|> try link <|> text)
( try (string "\n\n" >> return "")
<|> try (eof >> return "")
<|> try (lookAhead list >> return "")
)
text = do
text <-
many1Till
anyChar
( try (lookAhead (string "\n\n") >> return "")
<|> try
( lookAhead
( do
char '\n'
list
)
>> return ""
)
<|> try (lookAhead code >> return "")
<|> try (lookAhead link >> return "")
<|> (eof >> return "")
)
return $ Text (replace "\n" " " text)
code = do
try (char '`')
text <- many1Till (noneOf "\n") (try (char '`' >> return "") <|> (lookAhead (char '\n') >> return ""))
return $ Code text
inlineBlock = try code <|> try link <|> text
link = do
text <- many1Till letter (char '(')
number <- many1 digit
char ')'
return $ Link text (read number)
list = do
ws
char '-'
ws
item <- List <$> many1Till inlineBlock (char '\n')
optional (char '\n')
return item
many1Till ::
(Stream s m t, Show end) =>
ParsecT s u m a ->
ParsecT s u m end ->
ParsecT s u m [a]
many1Till a b = do
notFollowedBy b
x <- a
xs <- manyTill a b
return (x : xs)
wss x = ws *> x <* ws
ws :: Parser String
ws = many (oneOf "\n\t \r")
generateHTML :: [ManGroup] -> String
generateHTML = concatMap generateHTMLGroup
generateHTMLGroup :: ManGroup -> String
generateHTMLGroup (Title text) = "<h1>" ++ text ++ "</h1>"
generateHTMLGroup (Header text) = "<h2>" ++ text ++ "</h2>"
generateHTMLGroup (Paragraph blocks) = "<p>" ++ concatMap generateHTMLBlock blocks ++ "</p>"
generateHTMLGroup (List contents) = "<li>" ++ concatMap generateHTMLBlock contents ++ "</li>"
generateHTMLBlock :: InlineBlock -> String
generateHTMLBlock (Text text) = text
generateHTMLBlock (Code text) = "<code>" ++ text ++ "</code>"
generateHTMLBlock (Link text number) =
"<a href=\""
++ text
++ "."
++ show number
++ ".html\">"
++ text
++ "("
++ show number
++ ")</a>"
fromRight' :: Show a => Either a b -> b
fromRight' (Left l) = error ("Data.Either.Combinators.fromRight: " ++ show l)
fromRight' (Right x) = x
getAbsDirectoryContents :: FilePath -> IO [FilePath]
getAbsDirectoryContents dir =
listDirectory dir >>= mapM (canonicalizePath . (dir </>))
processManpage :: FilePath -> IO ()
processManpage path = do
markdown <- readFile path
template <- readFile "template.html"
let groups = fromRight' $ parse manpage path markdown
print groups
let html = generateHTML groups
let combined = replace "{{maincontent}}" html template
-- Write file to out/name.html
let outPath = replaceDirectory (path -<.> "html") "out"
writeFile outPath combined
main :: IO ()
main = do
-- Prepare output directory
removePathForcibly "out"
createDirectory "out"
-- Iterate over all files in static/
files <- getAbsDirectoryContents "static"
mapM_ (\y -> shelly $ do cp_r y "out") files
manpages <- getAbsDirectoryContents "manpages"
mapM_ processManpage manpages

+ 5
- 0
download_manpages.sh View File

@ -0,0 +1,5 @@
rm -rf manpages
git clone https://git.unix.lgbt/unix.lgbt/manpages.git
cd manpages
rm -rf $(find . -type f ! -name "*.md")
rm -rf .git

+ 73
- 0
man.cabal View File

@ -0,0 +1,73 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
name: man
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/man#readme>
homepage: https://github.com/githubuser/man#readme
bug-reports: https://github.com/githubuser/man/issues
author: Author name here
maintainer: example@example.com
copyright: 2021 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md
source-repository head
type: git
location: https://github.com/githubuser/man
library
other-modules:
Paths_man
hs-source-dirs:
src
build-depends:
MissingH
, base >=4.7 && <5
, directory
, filepath
, parsec
, shelly
default-language: Haskell2010
executable man-exe
main-is: Main.hs
other-modules:
Paths_man
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
MissingH
, base >=4.7 && <5
, directory
, filepath
, man
, parsec
, shelly
default-language: Haskell2010
test-suite man-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_man
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
MissingH
, base >=4.7 && <5
, directory
, filepath
, man
, parsec
, shelly
default-language: Haskell2010

+ 52
- 0
package.yaml View File

@ -0,0 +1,52 @@
name: man
version: 0.1.0.0
github: 'githubuser/man'
license: BSD3
author: 'Author name here'
maintainer: 'example@example.com'
copyright: '2021 Author name here'
extra-source-files:
- README.md
- ChangeLog.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/man#readme>
dependencies:
- base >= 4.7 && < 5
- parsec
- MissingH
- directory
- filepath
- shelly
library:
source-dirs: src
executables:
man-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- man
tests:
man-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- man

+ 68
- 0
stack.yaml View File

@ -0,0 +1,68 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/10.yaml
# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
extra-deps:
- MissingH-1.4.3.0@sha256:32f9892ec98cd21df4f4d3ed8d95a3831ae74287ea0641d6f09b2dc6ef061d39,4859
- random-1.1@sha256:7b67624fd76ddf97c206de0801dc7e888097e9d572974be9b9ea6551d76965df,1777
# Override default flag values for local packages and extra-deps
# flags: {}
# Extra package databases containing global packages
# extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.7"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor

+ 27
- 0
stack.yaml.lock View File

@ -0,0 +1,27 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages:
- completed:
hackage: MissingH-1.4.3.0@sha256:32f9892ec98cd21df4f4d3ed8d95a3831ae74287ea0641d6f09b2dc6ef061d39,4859
pantry-tree:
size: 5271
sha256: 6cd6a7213615d72a7db3234c7fcd4346ceb4889bc8fb14b302b5878768018fcd
original:
hackage: MissingH-1.4.3.0@sha256:32f9892ec98cd21df4f4d3ed8d95a3831ae74287ea0641d6f09b2dc6ef061d39,4859
- completed:
hackage: random-1.1@sha256:7b67624fd76ddf97c206de0801dc7e888097e9d572974be9b9ea6551d76965df,1777
pantry-tree:
size: 637
sha256: 14a1b01728c5584e87c9fa00746a66e28ffd89dd2c0eabd334c8463953496e1b
original:
hackage: random-1.1@sha256:7b67624fd76ddf97c206de0801dc7e888097e9d572974be9b9ea6551d76965df,1777
snapshots:
- completed:
size: 587546
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/10.yaml
sha256: 88b4f81e162ba3adc230a9fcccc4d19ac116377656bab56c7382ca88598b257a
original:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/10.yaml

BIN
static/gay.png View File

Before After
Width: 2  |  Height: 24  |  Size: 5.5 KiB

+ 46
- 0
static/style.css View File

@ -0,0 +1,46 @@
body {
background-color: lightgray;
font-family: sans-serif;
margin: 16px;
}
main,
header {
padding: 16px;
padding-top: 32px;
margin: auto;
left: 0;
right: 0;
max-width: 600px;
background-color: white;
background-image: url('gay.png');
background-repeat: repeat-x;
}
nav a {
margin: 8px;
padding: 4px 12px;
background-color: #888;
color: white;
text-decoration: none;
display: inline-block;
}
nav > a:hover {
background-color: #999;
}
header {
margin-bottom: 12px;
}
pre,
code {
display: inline;
font-size: 16px;
}
a {
color: #292929;
}

+ 18
- 0
template.html View File

@ -0,0 +1,18 @@
<!DOCTYPE html />
<html>
<head>
<title>unix.lgbt</title>
<meta name="description" content="unix.lgbt manpage viewer" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header>
<h1>Man Page Viewer</h1>
<nav>
<a href="/">Home</a>
</nav>
</header>
<main>{{maincontent}}</main>
</body>
</html>

+ 2
- 0
test/Spec.hs View File

@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Test suite not yet implemented"

Loading…
Cancel
Save