Skip to content

Commit

Permalink
Plain text mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Nov 9, 2021
1 parent 6fe82c5 commit 63589b1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ To be released.
- Since this version, it requires GHC 8.8.* at least, and supports GHC 9.0.*
at most.

- Now supports several content types besides HTML/XHTML. [[#18]]
- Now supports several content types besides HTML/XHTML. The below list
shows all supported content types: [[#18]]

- `text/html` (previously non-XHTML mode)
- `application/xhtml+xml` (previously XHTML mode)
- `text/plain` (added)

The below Haskell APIs changed:

Expand Down
15 changes: 13 additions & 2 deletions demo/src/Demo.elm
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type alias Source =
type ContentType
= Html
| Xhtml
| PlainText


type Options
Expand Down Expand Up @@ -250,6 +251,9 @@ makeInput source =

Xhtml ->
"appplication/xhtml+xml"

PlainText ->
"text/plain"
)
]
<|
Expand Down Expand Up @@ -1325,15 +1329,22 @@ viewOptions model =
Xhtml

_ ->
Html
PlainText
]
<|
[ Select.item
[ value "text/html", selected <| contentType == Html ]
[ text "HTML" ]
, Select.item
[ value "application/xhtml+xml", selected <| contentType == Xhtml ]
[ value "application/xhtml+xml"
, selected <| contentType == Xhtml
]
[ text "XHTML" ]
, Select.item
[ value "text/plain"
, selected <| contentType == PlainText
]
[ text "Plain text" ]
]
]
]
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ library:
- data-default >= 0.2 && < 1
- filepath >= 1 && < 2
- file-embed >= 0.0.10 && < 0.0.16
- html-entities >= 1 && < 2
when:
- condition: flag(static) || flag(embed-dictionary)
then:
Expand Down
7 changes: 6 additions & 1 deletion scripts/deno/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ export interface Preset {
*/
export type Dictionary = "kr-stdict";

export type ContentType =
| "text/html"
| "application/xhtml+xml"
| "plain/text";

/**
* Options for transformation.
* See also <https://github.com/dahlia/seonbi#http-api>.
*/
export interface Options {
/** Content type. */
contentType: "text/html" | "application/xhtml+xml";
contentType: ContentType;
/** Quoting options. */
quote:
| "CurvedQuotes"
Expand Down
17 changes: 17 additions & 0 deletions src/Text/Seonbi/ContentTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Text.Seonbi.ContentTypes
, TextTransformer
, asHtmlTransformer
, asHtmlTransformer'
, asPlainTextTransformer
, asXhtmlTransformer
, contentTypeFromText
, contentTypes
Expand All @@ -25,8 +26,11 @@ import Data.CaseInsensitive
import Data.Set
import Data.Text as ST
import Data.Text.Lazy as LT
import Data.Text.Lazy.Builder
import HTMLEntities.Decoder

import Text.Seonbi.Html
import qualified Text.Seonbi.Html.TagStack as TagStack

-- | Represents a function that transforms an 'HtmlEntity' list.
type HtmlTransformer m
Expand Down Expand Up @@ -73,6 +77,18 @@ asHtmlTransformer = asHtmlTransformer' False
asXhtmlTransformer :: (Monad m, MonadFail m) => TransformerTransformer m
asXhtmlTransformer = asHtmlTransformer' True

asPlainTextTransformer :: (Monad m, MonadFail m) => TransformerTransformer m
asPlainTextTransformer transformer text' = do
let entities = [HtmlCdata TagStack.empty $ LT.toStrict text']
output <- transformer entities
return . LT.concat $
[ case e of
HtmlCdata _ cdata -> LT.fromStrict cdata
HtmlText _ rawText' -> toLazyText $ htmlEncodedText rawText'
_ -> LT.empty
| e <- output
]

-- | Represents a case-insensitive content type.
type ContentType = CI ST.Text

Expand All @@ -91,6 +107,7 @@ transformers :: (Monad m, MonadFail m)
transformers =
[ ("text/html", TransformerTransformer' asHtmlTransformer)
, ("application/xhtml+xml", TransformerTransformer' asXhtmlTransformer)
, ("text/plain", TransformerTransformer' asPlainTextTransformer)
]

-- | Supported content types.
Expand Down
7 changes: 6 additions & 1 deletion test/Text/Seonbi/ContentTypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ spec = do
specify "asXhtmlTransformer" $ do
r <- asXhtmlTransformer textReverser "<p>foo <em>bar</em><br> baz</p>"
r `shouldBe` "<p> oof<em>rab</em><br/>zab </p>"
specify "asPlainTextTransformer" $ do
r <- asPlainTextTransformer textReverser
"<p>foo <em>bar</em><br> baz</p>"
r `shouldBe` ">p/<zab >rb<>me/<rab>me< oof>p<"
specify "transformWithContentType" $ do
let input = "<p>foo <em>bar</em><br></p>"
h <- transformWithContentType "text/html" textReverser input
h `shouldBe` "<p> oof<em>rab</em><br></p>"
x <- transformWithContentType "application/xhtml+xml" textReverser input
x `shouldBe` "<p> oof<em>rab</em><br/></p>"

p <- transformWithContentType "text/plain" textReverser input
p `shouldBe` ">p/<>rb<>me/<rab>me< oof>p<"

0 comments on commit 63589b1

Please sign in to comment.