在 Swift 中使用 Pandoc [英] Using Pandoc with Swift

查看:56
本文介绍了在 Swift 中使用 Pandoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pandoc 将 LaTeX 转换为 Markdown.我需要创建一个文件,然后运行 ​​pandoc 终端命令.问题是我创建的文件不在我运行终端命令的同一目录中.

I'm trying to use Pandoc to convert LaTeX to Markdown. I need to create a file and then run the pandoc terminal command. The problem is the file I create isn't in the same directory that I'm running the terminal commands in.

我尝试使用 shell("cd") 但它不会将您移动到用户的文件夹.

I tried using shell("cd") but it doesn't move you to the user's folder.

有什么想法吗?

import Cocoa

class ViewController: NSViewController {

    func shell(args: String...) -> Int32 {
        let task = NSTask()
        task.launchPath = "/usr/bin/env"
        task.arguments = args
        task.launch()
        task.waitUntilExit()
        return task.terminationStatus
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        shell("pwd")

        let file = "input.txt"
        let text = "\\emph{test}"

        if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
            let inputPath = dir.stringByAppendingPathComponent(file)

            //writing
            do {
                try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
                shell("pandoc","-f","latex","-t","markdown","input.txt","-o","output.txt")
            }
            catch {/* error handling here */}

            let outputPath = dir.stringByAppendingPathComponent("output.txt")
            //reading
            do {
                let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
                print(inputText)

                let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
                print(convertedText)

            }
            catch {/* error handling here */}
        }


    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

这是输出

/Users/james/Library/Developer/Xcode/DerivedData/FlashCardPreview-gqzwutewnxspazcdloxqruaikvel/Build/Products/Debug
env: pandoc: No such file or directory
\emph{test}

推荐答案

这部分是因为任务的可执行文件被设置为 env,它本身执行 pandoc;但与此同时它丢失了工作目录.

This is partly because the task's executable is set to env which itself executes pandoc; but in the meantime it loses the working directory.

对此的解决方案是将 launchPath 设置为 Pandoc 可执行文件.

The solution to this is to set launchPath to the Pandoc executable.

这也是因为我们必须在任务参数中使用 inputPathoutputPath 而不仅仅是文件名,来设置 currentDirectoryPath 用于任务.

This is also because we have to use inputPath and outputPath in the task arguments instead of just the filenames, or to set a currentDirectoryPath for the task.

工作版本 1:

func shell(args: String...) -> Int32 {
    let task = NSTask()
    task.launchPath = "/usr/local/bin/pandoc"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

let file = "input.txt"
let text = "\\emph{test}"

if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {

    let inputPath = dir.stringByAppendingPathComponent(file)
    let outputPath = dir.stringByAppendingPathComponent("output.txt")

    //writing
    do {
        try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
        shell("-f","latex","-t","markdown", inputPath, "-o", outputPath)
    }
    catch { print(error) }

    //reading
    do {
        let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
        print(inputText)

        let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
        print(convertedText)

    }
    catch { print(error) }
}

工作版本 2:

func shell(args: String...) -> Int32 {
    let task = NSTask()
    task.launchPath = "/usr/local/bin/pandoc"
    if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
        task.currentDirectoryPath = dir
    }
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

let file = "input.txt"
let text = "\\emph{test}"

if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {

    let inputPath = dir.stringByAppendingPathComponent(file)
    let outputPath = dir.stringByAppendingPathComponent("output.txt")

    //writing
    do {
        try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
        shell("-f","latex","-t","markdown","input.txt","-o","output.txt")
    }
    catch { print(error) }


    //reading
    do {
        let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
        print(inputText)

        let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
        print(convertedText)

    }
    catch { print(error) }
}

打印:

\emph{测试}
*测试*

\emph{test}
*test*

这篇关于在 Swift 中使用 Pandoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆