tbfisher / sublimetext-Pandoc

A Sublime Text plugin that uses Pandoc to convert text from one markup format into another. Pandoc can convert documents in markdown, reStructuredText, textile, HTML, DocBook, LaTeX, MediaWiki markup, OPML, or Haddock markup to XHTML, HTML5, HTML slide shows using Slidy, reveal.js, Slideous, S5, or DZSlides, Microsoft Word docx, OpenOffice/LibreOffice ODT, OpenDocument XML, EPUB version 2 or 3, FictionBook2, DocBook, GNU TexInfo, Groff man pages, Haddock markup, OPML, LaTeX, ConTeXt, LaTeX Beamer slides, PDF via LaTeX, Markdown, reStructuredText, AsciiDoc, MediaWiki markup, Emacs Org-Mode, Textile, or custom writers can be written in lua.
MIT License
137 stars 26 forks source link

Ability to create custom output formats #14

Closed py closed 10 years ago

py commented 10 years ago

I would like to create custom output formats available from the Sublime Text command palette. For example, I want to define "Pandoc: PDF-TOC" with something like this:

  "format_pdf-toc": {
    // use --latex-engine=engine where engine is one of pdflatex|lualatex|xelatex
    // This may need to be specified with a full path (e.g. on a mac with
    // BasicTeX "/usr/texbin/pdflatex").
    "to": ["--latex-engine=pdflatex", "-s", "--toc", "--number-sections"]
  }

I've tried adding new formats and format configs to pandoc.sublime-settings, but have not yet had success. Does doing something like this require changes to pandoc.py?

py commented 10 years ago

After studying the package some more, I think that the formats[] and format_configs in the pandoc.sublime_settings can be combined into something like this:

{
  // Sets the path to the pandoc binary.
  "pandoc-path": null,

  // Opens output in new buffer (instead of replacing input in same)
  "new-buffer": 1,

  // configure Pandoc executable
  // see http://johnmacfarlane.net/pandoc/README.html#options

  // enabled Pandoc formats
  // key: label, value: Pandoc format key (-f or -t values)
  // Format label -> parameters
  "formats": {
    "PDF": {          // label
      "fkey": "pdf",  // pandoc format key
      "to": ["--latex-engine=C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/pdflatex.exe"]
    },
    "PDF with TOC": {
      "fkey": "pdf",
      "to": ["--latex-engine=C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/pdflatex.exe", "-s", "--toc"]
    }
  }
}

Making the pandoc format key a parameter allows of the creation of multiple configs for each document format type. All of these show up in the quick panel, then run the right pandoc configs. 20140116_18-50-48

I'm by no means a python or Sublime Text expert, so it's taking me a long time and much trial and error to rewire pandoc.py to handle the new settings file.

Does this sound like a viable way to proceed, or am I missing something that makes it a bad plan?

tbfisher commented 10 years ago

Sounds viable to me.

You may want to look at

https://sublime.wbond.net/packages/Pandown

which is a more sophisticated and allows you to define arbitrary transformations.

tbfisher commented 10 years ago

I just tried Pandown (again) and didn't like it for my workflow. I think this plugin can compliment Pandown, if it's easy to use and has a quick and simple UX.

I like your idea, and incorporated it into a port of this plugin for sublime 3 -- see f5cdc73

py commented 10 years ago

I agree with you re. Pandown - it may be preferable for doing a thesis or large document, but seems to formal for quick documents. I'm excited you like the idea and thank you for making it real.

I've been trying to test out the new version, but no success yet. Does the new version only work with sublime 3? I installed sublime 3 and your package, but get the following errors:

Traceback (most recent call last):
  File "X/subprocess.py", line 1090, in _execute_child
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 543, in run_
    return self.run(edit, **args)
  File "Pandoc in C:\Users\Peter\AppData\Roaming\Sublime Text 3\Installed Packages\Pandoc.sublime-package", line 116, in run
  File "X/subprocess.py", line 818, in __init__
  File "X/subprocess.py", line 1096, in _execute_child
PermissionError: [WinError 5] Access is denied

I'm on Windows 7 x64 Sublime build 3059.

tbfisher commented 10 years ago

Yes, sublime 3 only.

Seems the plugin is having issues finding pandoc. Try updating the plugin, and check the pandoc-path setting

I'll install on windows tonight and test this out.

tbfisher commented 10 years ago

I was able to run on windows ok, had to create a user settings file

{
    "user": {
        "pandoc-path": "C:/Users/brian/AppData/Local/Pandoc/pandoc.exe"
    }
}

After installing pandoc, I used the where command to find the executable path

where pandoc
py commented 10 years ago

Alright - that worked. I can generate most formats as outputs, but PDF is still having some issues. I get this error:

error: pandoc.exe: cannot produce pdf output with pdf writer

Here is my user/pandoc.sublime-settings:

{
  "user": {
    "pandoc-path": "C:/Users/peter/AppData/Local/Pandoc/pandoc.exe",

    // Transformations
    "transformations": {
      "PDF2": {
        "scope": {
          "text.html": "html",
          "text.html.markdown": "markdown",
          },
        "pandoc-arguments": [
          "-V geometry:margin=1in", 
          "-s", "--toc", "--number-sections",
          "-t", "pdf",
          "--latex-engine=C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/pdflatex.exe"
        ],
      },
    }
  }
}

I think the problem is related to this pandoc issue: https://github.com/jgm/pandoc/issues/571

I looked at my config for the sublimetext-Pandoc package for ST2, and it generated pdfs just fine as long as I included the --latex-engine=path/to/pdflatex.exe as a to parameter; no -t pdf necessary.

I know you had to change the guts of the package to accommodate arbitrary transformations, but is there a way to not require the -t command when writing to a temp file, similar to the old version for ST2?

Also, it might help users to add print cmd toward the end of PandocCommand so that we can see how our transformation definitions are converted to the actual pandoc commands. Then I won't have to bug you so much.

Sorry to be so demanding - I'm trying to understand how ST3 packages work so I can help fix some of this myself at some point. Thanks again for updating the package to incorporate my original request on this issue.

tbfisher commented 10 years ago

thanks, removing -t pdf fixed pdf output for me. Pushed a fix.

That's kinda annoying... I just wanted users to be able to configure -t and the plugin would just read the value... now I'm taking -t out in this case.

added printing the command to the console.

If you configure any other transformations, please consider contributing them. I would like the plugin to work out-of-the-box with a bunch of formats.

py commented 10 years ago

Alright - we're making pdfs! I'll fork, add some formats, then create a pull request. I think I'll add some documentation as well to help others avoid my pitfalls.

Another thing to consider adding: make the --latex-engine a global variable, similar to the pandoc path. I haven't thought through all the ramifications to the logic, but it would be nice to get that variable out of the way when people first install the package and create their user-settings files. After that it should "just work".

The logic in your program for pdf output would need to add something like the following to auto-include the --latex-engine pandoc option.

cmd = cmd + "--latex-engine=" + pdf_latex_path
py commented 10 years ago

Brian - I've got some transformations built. Want to confirm - I should put them in pandoc.sublime-settings, correct?

On Wed, Jan 29, 2014 at 12:26 AM, Brian Fisher notifications@github.comwrote:

thanks, removing -t pdf fixed pdf output for me. Pushed a fix.

That's kinda annoying... I just wanted users to be able to configure -tand the plugin would just read the value... now I'm taking -t out in this case.

added printing the command to the console.

If you configure any other transformations, please consider contributing them. I would like the plugin to work out-of-the-box with a bunch of formats.

Reply to this email directly or view it on GitHubhttps://github.com/tbfisher/sublimetext-Pandoc/issues/14#issuecomment-33559824 .

tbfisher commented 10 years ago

Great thanks, and yes, place them in the default settings file.