FastCGI Haskell 脚本利用 Pandoc 文本转换

发布于 2024-10-25 10:25:29 字数 1596 浏览 1 评论 0原文

1. 动机

我正在编写我自己的迷你维基。我希望能够轻松地从 Markdown 转换为 LATEX/HTML,反之亦然。经过一番搜索,我发现 Pandoc,它是用 Haskell 编写的,我可以使用 FastCGI 模块来运行我的 Apache 服务器上的 Haskell 程序。

2. 问题/疑问

我不确定应该使用什么协议向 FastCGI 脚本发送输入/输出变量(POST/GET?)以及具体是如何完成的。有什么想法、建议、解决方案吗?

3. 采取的步骤

3.1 尝试

这是我到目前为止所做的(基于示例代码)。 请注意,我没有 Haskell 经验,目前我没有太多时间学习该语言。我只是希望能够使用 pandoc 文本格式转换工具。

module Main ( main ) where

import Control.Concurrent
import Network.FastCGI
import Text.Pandoc

--initialize Variables/ functions
fastcgiResult :: CGI CGIResult
markdownToHTML:: String -> String

--implement conversion function
markdownToHTML s = writeLaTeX defaultWriterOptions {writerReferenceLinks = True} (readMarkdown defaultParserState s)    

--main action
fastcgiResult = do
    setHeader "Content-type" "text/plain"
    n <- queryString
    output $ (markdownToHTML n)

main :: IO ()
main = runFastCGIConcurrent' forkIO 10 fastcgiResult

此代码读取请求 url 中问号后面的字符串。但这不是一个好的解决方案,因为某些字符被省略(例如 '#' )并且空格被替换为“/20%”。

提前致谢。

3.2 Network.CGI

文档可在此处找到。 在“输入”标题下有许多获取输入的方法。 哪一个适合我?

是:

获取输入变量的值,例如从表单中获取。如果变量有多个值,则返回第一个值。示例:

query <- getInput "query"

假设我有一个带有 name='Joe' 的 HTML POST 表单,我可以使用 getInput 获取它吗?如果是这样,我该如何处理 Maybe String 类型?

1. Motivation

I'm writing my own mini-wiki. I want to be able to easily convert from markdown to LATEX/HTML and vice versa. After some searching I discovered Pandoc, which is written in Haskell and that I could use the FastCGI module to run a Haskell program on my Apache server.

2. Problem/ Question

I'm not sure how to what protocol I should use to send my FastCGI script the input/output variables (POST/GET?) and how this is done exactly. Any ideas, suggestions, solutions?

3. Steps taken

3.1 Attempt

Here is what I've done so far (based on example code). Note, I have no experience in Haskell and at the moment I don't have too much time to learn the language. I'd just love to be able to use the pandoc text format conversion tool.

module Main ( main ) where

import Control.Concurrent
import Network.FastCGI
import Text.Pandoc

--initialize Variables/ functions
fastcgiResult :: CGI CGIResult
markdownToHTML:: String -> String

--implement conversion function
markdownToHTML s = writeLaTeX defaultWriterOptions {writerReferenceLinks = True} (readMarkdown defaultParserState s)    

--main action
fastcgiResult = do
    setHeader "Content-type" "text/plain"
    n <- queryString
    output $ (markdownToHTML n)

main :: IO ()
main = runFastCGIConcurrent' forkIO 10 fastcgiResult

This code reads the string after the question mark in the request url. But this is not a good solution as certain characters are omitted (e.g. '#' ) and spaces are replaced by "/20%".

Thanks in advance.

3.2 Network.CGI

Documentation found here. Under the heading "Input" there are a number of methods to get input. Which one is right for me?

Is it :

Get the value of an input variable, for example from a form. If the variable has multiple values, the first one is returned. Example:

query <- getInput "query"

So lets say I have a HTML POST form with name='Joe' can I grab this using getInput? And if so how do I handle the Maybe String type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-11-01 10:25:29

fastCGI包实际上是cgi包的扩展,其中包括协议类型用于接收请求数据并返回结果页面。我建议首先使用 CGI,然后在知道自己在做什么后转向 fastCGI。

您可能还想查看本教程

编辑以回答有关本教程的问题:

“Maybe a”是一种可以包含“Just a”或“Nothing”的类型。大多数语言使用空指针来表示没有数据,但 Haskell 没有空指针。因此,对于数据可能为空的情况,我们有一个明确的“也许”类型。两个构造函数(“Just”和“Nothing”)以及类型强制您在可能发生时显式允许 null 情况,但也让您在不可能发生时忽略它。

“maybe”函数是 Maybe 类型的通用提取器。签名是:

maybe :: b -> (a -> b) -> Maybe a -> b

从前到后获取参数,“也许”第三个参数是您尝试使用的值。第二个参数是一个函数,如果第三个参数是“Just v”,则调用该函数,在这种情况下结果是“f v”。第一个参数是默认值,如果第三个参数为“Nothing”则返回。

在这种情况下,技巧是“cgiMain”函数被调用两次。如果它找到输入字段“name”,则“mn”变量将被设置为(Just“Joe Bloggs”),否则它将被设置为(Nothing)。 (我现在使用括号来分隔值,因为引号用于字符串)。

因此“也许”调用返回要呈现的页面。第一次没有提供名称,因此“mn”是(Nothing),并且返回默认的“inputForm”页面进行渲染。当用户单击“提交”时,会请求相同的 URL,但这次设置了“名称”字段,因此现在您将获得以名称作为参数调用的“greet”函数,因此它会显示“Hello Joe Bloggs”。

The fastCGI package is actually a extension of the cgi package, which includes the protocol types for receiving request data and returning result pages. I'd suggest using CGI to start with, and then move to fastCGI once you know what you are doing.

You might also want to look at this tutorial.

Edit to answer questions about the tutorial:

"Maybe a" is a type that can either contain "Just a" or "Nothing". Most languages use a null pointer to indicate that there is no data, but Haskell doesn't have null pointers. So we have an explicit "Maybe" type instead for cases when the data might be null. The two constructors ("Just" and "Nothing") along with the type force you to explicitly allow for the null case when it might happen, but also let you ignore it when it can't happen.

The "maybe" function is the universal extractor for Maybe types. The signature is:

maybe :: b -> (a -> b) -> Maybe a -> b

Taking the arguments from front to back, the "Maybe a" third argument is the value you are trying to work with. The second argument is a function called if the third argument is "Just v", in which case the result is "f v". The first argument is the default, returned if the third is "Nothing".

In this case, the trick is that the "cgiMain" function is called twice. If it finds an input field "name" then the "mn" variable will be set to (Just "Joe Bloggs"), otherwise it will be set to (Nothing). (I'm using brackets to delimit values now because quotes are being used for strings).

So the "maybe" call returns the page to render. The first time through no name is provided, so "mn" is (Nothing) and the default "inputForm" page is returned for rendering. When the user clicks Submit the same URL is requested, but this time with the "name" field set, so now you get the "greet" function called with the name as an argument, so it says "Hello Joe Bloggs".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文