Skip to content

Commit

Permalink
Add contentType to HTTP responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Nov 12, 2021
1 parent 63589b1 commit edc3f6e
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 73 deletions.
6 changes: 4 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ To be released.
The below HTTP APIs changed:

- Added an optional field `"contentType"` with the default value
`"text/html"`.
- Removed `"xhtml"` field in favour of new `"contentType"` field.
`"text/html"` to requests.
- Removed `"xhtml"` field in favour of new `"contentType"` field
from requests.
In order to use XHTML mode, configure `"contentType"` field with
`"application/xhtml+xml"`.
- Added `"contentType"` field to responses.

- Added `Text.Seonbi.Html.Lang` module.

Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ perform the following transformations:
- A hyphen or an equality symbol wrapped by inequality symbols (`<->`, `<=>`)
into bi-directional arrows (``, ``)

Since its transformations work in HTML-level, it also plays well with web
markup languages like CommonMark, Markdown, and Textile. In a similar way to
SmartyPants, it does not modify characters within several sensitive
HTML elements like `<pre>`/`<code>`/`<script>`/`<kbd>`.
Each transformations can be partially turned on and off, and some
transformations have many options.

All transformations work with both plain texts and rich text tree.
In a similar way to SmartyPants, it does not modify characters within
several sensitive HTML elements like `<pre>`/`<code>`/`<script>`/`<kbd>`.
Chinese/Japanese stops or hanzi/kanji characters inside elements with
`lang="zh"`/`lang="ja"`[^1] are never transformed.

[^1]: Technically, only Korean contents and language-unspecified elements
are transformed. Elements having `lang` attribute with language tags
referring to any Korean language are treated as Korean contents,
e.g., `ko`, `ko-Hang`, `kor-KP`, `kor-Kore`.

[releases]: https://github.com/dahlia/seonbi/releases
[releases-badge]: https://img.shields.io/github/v/release/dahlia/seonbi
Expand Down Expand Up @@ -148,6 +157,7 @@ The following is an example request:

{
"preset": "ko-kr",
"contentType": "text/html",
"sourceHtml": "<p>하늘과 바람과 별과 詩</p>"
}

Expand All @@ -159,6 +169,7 @@ The HTTP API server would respond like this:

{
"success": true,
"contentType": "text/html",
"resultHtml": "<p>하늘과 바람과 별과 시</p>"
}

Expand Down
6 changes: 4 additions & 2 deletions app/seonbi-api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ instance FromJSON Input where
parseJSON = withObject "Input" $ \ v -> do
sourceHtml' <- v .: "sourceHtml"
preset <- v .:? "preset"
contentType' <- v .:? "contentType" .!= "text/html"
config <- case preset of
Just locale ->
let presets' = presets :: M.Map Text (Configuration IO ())
Expand All @@ -51,7 +52,6 @@ instance FromJSON Input where
, intercalate ", " (M.keys presets')
]
Nothing -> do
contentType' <- v .:? "contentType" .!= "text/html"
quote' <- v .:? "quote"
cite' <- v .:? "cite"
arrow' <- v .:? "arrow"
Expand All @@ -70,7 +70,7 @@ instance FromJSON Input where
, stop = stop'
, hanja = hanja'
}
return $ Input sourceHtml' config
return $ Input sourceHtml' $ config { contentType = contentType' }

instance FromJSON ContentType where
parseJSON = withText "ContentType" $ \ t ->
Expand Down Expand Up @@ -134,6 +134,8 @@ app AppOptions { allowOrigin, debugDelayMs } request respond =
respond' status200 $ object
[ "success" .= Bool True
, "resultHtml" .= String result
, "contentType" .= String
(contentTypeText $ contentType config)
]
Left msg -> respond' status400 $ object
[ "success" .= Bool False
Expand Down
2 changes: 1 addition & 1 deletion demo/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"elm/url": "1.0.0",
"hecrj/html-parser": "2.3.4",
"pablohirafuji/elm-markdown": "2.0.5",
"pablohirafuji/elm-syntax-highlight": "3.1.0",
"pablohirafuji/elm-syntax-highlight": "3.4.1",
"rundis/elm-bootstrap": "5.1.0"
},
"indirect": {
Expand Down
Loading

0 comments on commit edc3f6e

Please sign in to comment.