Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown #76

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions plugins/markdown/lib/markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "java"
require "rexml/document"
#load a markdown4j jar
#depends: https://code.google.com/p/markdown4j/
Dir[File.join(Redcar.root, %w(plugins markdown vendor *.jar))].each {|jar| require jar }

module Redcar
class Markdown
def self.keymaps
map = Redcar::Keymap.build("main", [:osx, :linux, :windows]) do
link "Alt+Shift+Q", MarkdownPreviewCommand
end
[map]
end

def self.menus
Redcar::Menu::Builder.build do
sub_menu "File" do
item "Markdown Preview", :command => MarkdownPreviewCommand, :priority => 8
end
end
end

class MarkdownPreviewCommand < Redcar::EditTabCommand
def execute
begin
mirror, text = doc.mirror, doc.get_all_text

name, project_path = if mirror && path = mirror.path && File.exists?(mirror.path)
["Preview: #{File.basename(mirror.path)}", File.dirname(mirror.path)]
else
["Preview", Dir.home]
end

source = MarkdownConverter::Markdown4jProcessor.new.process(text)

preview = java.io.File.createTempFile("preview",".html")
preview.deleteOnExit
path = preview.get_absolute_path

doc = REXML::Document.new("<html>#{source}</html>")

REXML::XPath.each(doc, "//img") do |img|
src = img.attribute("src")
if src && src.value && !src.value.start_with?("http")
img.add_attribute("src", File.absolute_path("#{project_path}/#{src.value}"))
end
end

File.open(path,'w') {|f| f.puts(doc)}

Redcar::HtmlView::DisplayWebContent.new(name, path, false).run
rescue Exception => e
Application::Dialog.message_box("Something went wrong :(")
end
end
end

module MarkdownConverter
include_package "org.markdown4j"
end
end
end
8 changes: 8 additions & 0 deletions plugins/markdown/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Plugin.define do
name "markdown"
version "0.1"
file "lib", "markdown"
object "Redcar::Markdown"
dependencies "application", ">0",
"HTML View", ">=0.3.2"
end
Binary file added plugins/markdown/vendor/markdown4j-2.2.jar
Binary file not shown.