Home Download Buy Blog Forum Support

Markdown Preview

Markdown Preview

Postby forehead on Tue Oct 11, 2011 5:56 am

I hacked a Markdown Build System that does a pretty barebones Markdown-to-HTML preview. It depends on RVM and Ruby and the rdiscount gem.

Since we don't apparently have access to real environment environment variables in config files, I had to hardcode my paths. Here's the code if anyone wants to play around:

Markdown.build-system
Code: Select all
{
  "cmd": ["/Users/alex/.rvm/bin/rvm-exec ~/bin/mdpreview $file"],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.markdown",
  "shell": true
}


~/bin/mdpreview:
Code: Select all
#!/usr/bin/env ruby

begin
  require 'rdiscount'
rescue LoadError
  `gem install rdiscount`
  require 'rdiscount'
end

rdiscount="/Users/alex/.rvm/gems/ruby-1.9.2-p290/bin/rdiscount"

input = ARGV[0]
input_dir = File.dirname input

require 'rdiscount'
markdown = RDiscount.new(File.read(input))
html = markdown.to_html

page = <<HTML
<head>
<style>
body {
  font-family: georgia, arial, helvetica;
  padding: 1em;
}
p {
  max-width: 40em;
}
h1 {
  background-color: #f1f1fd;
  border-bottom: 1px solid blue;
  padding: .25em 1em;
  margin: 1em -1em .25em;
}
</style>
</head>
<body>
  #{html}
HTML

html_file = File.basename(input).gsub(/\.md$/, '.html')
output = "#{input_dir}/#{html_file}"

File.open(output, "w") do |f|
  f.write(page)
end
cmd = "open #{output}"
puts `#{cmd}`


Usage:

* select Tools>Build System>Markdown
* hit cmd-B

Problems:

* Uses Ruby, not Python which would be more ST2ish
* Uses RVM; doesn't work with system ruby if you'd need sudo to install gems
* May only work with Ruby 1.9.2
* Hardcoded paths galore
* I wasn't sure where to put the executable file, so I put it in ~/bin
* HTML preview opens in a new browser tab every time
* only works on MacOS, though someone on Linux could probably find an equivalent to "open"
* don't know how to get it to automatically recognize markdown files ("selector: source.markdown" doesn't do it)
* (over)writes foo.html in same dir as foo.md (bug or feature?)
forehead
 
Posts: 20
Joined: Wed Oct 05, 2011 10:32 pm

Re: Markdown Preview

Postby bigfudge on Mon Oct 31, 2011 12:23 pm

Hi - thanks for this, but I think the scope selector should be:
Code: Select all
text.html.markdown


This works better for me anyhow - it means you can leave the build system on automatic, and the filetypes of .md, .mdown, .markdown etc all get picked up automatically.
bigfudge
 
Posts: 3
Joined: Wed Aug 10, 2011 8:03 am

Re: Markdown Preview

Postby surg on Sun Nov 20, 2011 4:41 pm

Hi!
Thanks for the post.
Based on it and a python library markdown2, ST2ize your solution :)
Here's my build-system definition:
Code: Select all
{
   "cmd": ["python ~/Library/Python/2.7/bin/markdown2 $file"],
     "selector": "text.html.markdown",
     "shell": true
}
surg
 
Posts: 1
Joined: Sun Nov 20, 2011 4:36 pm

Re: Markdown Preview

Postby mmerline on Mon Jan 16, 2012 8:25 pm

I would love to see this implemented into the program. I would probably convert to using Sublime as my primary text editor.
mmerline
 
Posts: 4
Joined: Mon Jan 16, 2012 8:23 pm

Re: Markdown Preview

Postby ksi on Thu Feb 16, 2012 9:47 pm

New to ST2, I wonder how I can actually see the markdown preview.
I created the mentioned Markdown.build-system, installed markdown2 python script, set Build Type to auto and Hit Cmd-B with a md-file open.

At the bottom of ST2 I see the html output being produced and printed, but that's far away from a preview.
What am I doing wrong?
ksi
 
Posts: 1
Joined: Thu Feb 16, 2012 9:42 pm

Re: Markdown Preview

Postby seth007 on Wed Mar 21, 2012 9:02 am

seth007
 
Posts: 8
Joined: Tue Mar 20, 2012 9:54 pm

Re: Markdown Preview

Postby abathologist on Thu Mar 22, 2012 1:22 am

Is it already known that, for mac users, you can have ST2 build the mmd document to Marked.app? (http://support.markedapp.com/kb/how-to- ... nd-bundles).

Once you've opened it in Marked, it will refresh every time you save (i.e., assuming you've set "save on loss of focus" to true, every time you switch windows).

Or perhaps I don't quite understand the gist here...
abathologist
 
Posts: 18
Joined: Thu Mar 15, 2012 1:29 am

Re: Markdown Preview

Postby seth007 on Thu Mar 22, 2012 11:52 am

abathologist wrote:Is it already known that, for mac users, you can have ST2 build the mmd document to Marked.app? (http://support.markedapp.com/kb/how-to- ... nd-bundles).

Once you've opened it in Marked, it will refresh every time you save (i.e., assuming you've set "save on loss of focus" to true, every time you switch windows).

Or perhaps I don't quite understand the gist here...


I think the gist is that if you are not using Mac, this won't work. :)

But for mac users, yes that’s the best solution.
seth007
 
Posts: 8
Joined: Tue Mar 20, 2012 9:54 pm


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests