Using Pandoc with Swift

前端 未结 1 1675
无人及你
无人及你 2021-01-22 17:50

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 d

相关标签:
1条回答
  • 2021-01-22 18:13

    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.

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

    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.

    Working version 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
    }
    

    and

    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) }
    }
    

    Working version 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
    }
    

    and

    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) }
    }
    

    Prints:

    \emph{test}
    *test*

    0 讨论(0)
提交回复
热议问题