Cocoa 应用程序最简单的 Markdown 实现是什么?

发布于 2024-08-04 12:15:36 字数 446 浏览 12 评论 0原文

我正在用 Objective-C 编写一个 Cocoa 应用程序,并且我希望能够合并 Markdown。用户以 Markdown 语法输入文本,单击“导出”按钮,程序将输出 XHTML 文件。

不过,似乎有很多选择。我可以使用 C/C++ 实现 之一,我可以将 Perl 脚本作为我的 Cocoa 应用程序的资源运行,我假设可以使用 Python 实现和 PyObjC 桥或 Perl实现和 CamelBones 或 PerlObjC 桥接器。最简单、最容易的解决方案是什么?我没有做任何复杂的事情,比如需要线程的实时渲染预览。

I'm writing a Cocoa application in Objective-C, and I would like to be able to incorporate Markdown. The user will enter text in Markdown syntax, click an "export" button, and the program will output a file of XHTML.

It seems like there are a lot of options, though. I could use one of the C/C++ implementations, I could run the Perl script as a resource to my Cocoa app, I assume could use the Python implementation and the PyObjC bridge or the Perl implementation and the CamelBones or PerlObjC bridges. What would be the simplest and easiest solution? I'm not doing anything complicated like a real-time rendered preview that would require threading.

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

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

发布评论

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

评论(6

明月松间行 2024-08-11 12:15:36

我查看了各种选项,最后发现了libsoldout,一个非常小的C非常容易集成的实现。你只需要在你的 Xcode 项目中包含 array.[ch]、buffer.[ch]、markdown.[ch] 和 renderers.[ch],然后你就可以将 NSString 从 markdown 转换为 HTML,如下所示:

NSString *rawMarkdown;
const char * prose = [rawMarkdown UTF8String];  
struct buf *ib, *ob;       

int length = rawMarkdown.length + 1;

ib = bufnew(length);
bufgrow(ib, length);
memcpy(ib->data, prose, length);
ib->size = length;

ob = bufnew(64);
markdown(ob, ib, &mkd_xhtml);

NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
NSLog(@"%@", shinyNewHTML);

bufrelease(ib);
bufrelease(ob);

I had a look at the various options, and in the end found libsoldout, a very small C implementation that's quite easy to integrate. You just need to include array.[ch], buffer.[ch], markdown.[ch], and renderers.[ch] in your Xcode project, then you can convert an NSString from markdown to HTML like so:

NSString *rawMarkdown;
const char * prose = [rawMarkdown UTF8String];  
struct buf *ib, *ob;       

int length = rawMarkdown.length + 1;

ib = bufnew(length);
bufgrow(ib, length);
memcpy(ib->data, prose, length);
ib->size = length;

ob = bufnew(64);
markdown(ob, ib, &mkd_xhtml);

NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
NSLog(@"%@", shinyNewHTML);

bufrelease(ib);
bufrelease(ob);
忆沫 2024-08-11 12:15:36

我刚刚在 iPad 应用程序中使用了 Sundown 实现,其中包括 SmartyPants 支持,并取得了巨大成功。花费了大约 15 分钟来构建一个测试应用程序。

假设您有一个 UITextView *textView (您设置了Delegate:self)和一个 UIWebView *webView 用于显示结果:

- (void) textViewDidEndEditing:(UITextView *)textView
{
    NSString *rawMarkdown = [textView text];
    const char * prose = [rawMarkdown UTF8String];  
    struct buf *ib, *ob;       

    int length = [rawMarkdown lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;

    ib = bufnew(length);
    bufgrow(ib, length);
    memcpy(ib->data, prose, length);
    ib->size = length;

    ob = bufnew(64);

    struct sd_callbacks callbacks;
    struct html_renderopt options;
    struct sd_markdown *markdown;


    sdhtml_renderer(&callbacks, &options, 0);
    markdown = sd_markdown_new(0, 16, &callbacks, &options);

    sd_markdown_render(ob, ib->data, ib->size, markdown);
    sd_markdown_free(markdown);


    NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
    [webView loadHTMLString:shinyNewHTML baseURL:[[NSURL alloc] initWithString:@""]];

    bufrelease(ib);
    bufrelease(ob);
}

I just used the Sundown implementation which includes SmartyPants support, in an iPad app with great success. Took about 15 minutes to build a test app.

Assume you have a UITextView *textView (which you setDelegate:self) and also a UIWebView *webView in which to display the results:

- (void) textViewDidEndEditing:(UITextView *)textView
{
    NSString *rawMarkdown = [textView text];
    const char * prose = [rawMarkdown UTF8String];  
    struct buf *ib, *ob;       

    int length = [rawMarkdown lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;

    ib = bufnew(length);
    bufgrow(ib, length);
    memcpy(ib->data, prose, length);
    ib->size = length;

    ob = bufnew(64);

    struct sd_callbacks callbacks;
    struct html_renderopt options;
    struct sd_markdown *markdown;


    sdhtml_renderer(&callbacks, &options, 0);
    markdown = sd_markdown_new(0, 16, &callbacks, &options);

    sd_markdown_render(ob, ib->data, ib->size, markdown);
    sd_markdown_free(markdown);


    NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
    [webView loadHTMLString:shinyNewHTML baseURL:[[NSURL alloc] initWithString:@""]];

    bufrelease(ib);
    bufrelease(ob);
}
明月松间行 2024-08-11 12:15:36

您可能想查看我编写的开源应用程序 Macdown (或者也可以使用rentzsch的Markdownlive),这两个应用程序将此功能合并为唯一目的。

You may want to check out the open-source app Macdown which I wrote (or alternatively rentzsch's Markdownlive), which incorporate this functionality as the sole purpose of the two apps.

负佳期 2024-08-11 12:15:36

我发现使用这些基于 C 的库处理大量 Markdown 时存在问题。

这里有一个非常简单的 Obj-C 库对我有用:

https://github.com/mdiep/MMMarkdown


使用 MMMarkdown 的步骤:

  1. 构建 OS X 或 iOS 目标

  2. 复制 include/MMMarkdown.h 以及
    lib/libMMMarkdown-Mac.alib/libMMMarkdown-iOS.a 到您的项目

  3. 然后代码是:

#import "MMMarkdown.h"

NSError  *error;
NSString *markdown   = @"# Example\nWhat a library!";
NSString *htmlString = [MMMarkdown HTMLStringWithMarkdown:markdown error:&error];
// Returns @"<h1>Example</h1>\n<p>What a library!</p>"

I found problems with processing large amounts of markdown with these C-based libraries.

There's a very simple Obj-C library that worked for me here:

https://github.com/mdiep/MMMarkdown


Steps to use MMMarkdown:

  1. Build the OS X or iOS target

  2. Copy include/MMMarkdown.h and either
    lib/libMMMarkdown-Mac.a or lib/libMMMarkdown-iOS.a into your project

  3. Then the code is:

#import "MMMarkdown.h"

NSError  *error;
NSString *markdown   = @"# Example\nWhat a library!";
NSString *htmlString = [MMMarkdown HTMLStringWithMarkdown:markdown error:&error];
// Returns @"<h1>Example</h1>\n<p>What a library!</p>"
甜味拾荒者 2024-08-11 12:15:36

我使用过peg-markdown,它比原来的perl快得多,并且可以处理一些语法扩展(如果启用)。

I've used peg-markdown, it's much faster than the original perl and can handle a few syntax extensions if you enable them.

悲凉≈ 2024-08-11 12:15:36
  1. Oliver Letterer 的 GHMarkdownParser 将 Markdown 转换为 HTML。
  2. Phil Toland 的 QLMarkdown 用于 Markdown 文件的 QuickLook 生成器。
  1. Oliver Letterer's GHMarkdownParser translate markdown to HTML.
  2. Phil Toland's QLMarkdown QuickLook generator for markdown files.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文