Skip to content

Commit

Permalink
add "copy as html", remove unused command
Browse files Browse the repository at this point in the history
  • Loading branch information
timonwong committed Mar 14, 2013
1 parent ab5028d commit 29f445d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
12 changes: 10 additions & 2 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{ "caption": "-", "id": "file" },
{ "command": "omni_markup_preview", "caption": "Preview Current File in Browser" }
]
{ "command": "omni_markup_preview", "caption": "Preview Markup in Browser" },
{ "command": "omni_markup_export", "caption": "Export Markup as HTML" },
{
"command": "omni_markup_export",
"caption": "Copy Markup as HTML",
"args": {
"clipboard_only": true
}
}
]
5 changes: 5 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
{
"keys": ["ctrl+alt+x"], "command": "omni_markup_export",
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
},
{
"keys": ["ctrl+alt+c"], "command": "omni_markup_export",
"args": { "clipboard_only": true },
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
}
]
5 changes: 5 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
{
"keys": ["super+alt+x"], "command": "omni_markup_export",
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
},
{
"keys": ["ctrl+alt+c"], "command": "omni_markup_export",
"args": { "clipboard_only": true },
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
}
]
5 changes: 5 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
{
"keys": ["ctrl+alt+x"], "command": "omni_markup_export",
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
},
{
"keys": ["ctrl+alt+c"], "command": "omni_markup_export",
"args": { "clipboard_only": true },
"context": [{"key": "omnimarkup_is_enabled", "operator": "equal", "operand": ""}]
}
]
8 changes: 1 addition & 7 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@
"caption": "OmniMarkupPreviewer: Export to html File",
"command": "omni_markup_export"
},
{
"caption": "OmniMarkupPreviewer: Sweep Cache (Remove Unused)",
"command": "omni_markup_clean_cache",
"args": {"remove_all": false}
},
{
"caption": "OmniMarkupPreviewer: Empty Cache",
"command": "omni_markup_clean_cache",
"args": {"remove_all": true}
"command": "omni_markup_clean_cache"
}
]
80 changes: 44 additions & 36 deletions OmniMarkupPreviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,54 +117,62 @@ def is_enabled(self):


class OmniMarkupCleanCacheCommand(sublime_plugin.ApplicationCommand):
def run(self, remove_all=False):
def run(self):
storage = RenderedMarkupCache.instance()
if remove_all:
storage.clean()
return
keep_ids_list = []
for window in sublime.windows():
for view in window.views():
keep_ids_list.append(view.buffer_id())
storage.clean(keep_ids=set(keep_ids_list))
storage.clean()


class OmniMarkupExportCommand(sublime_plugin.TextCommand):
def run(self, edit):
def copy_to_clipboard(self, html_content):
sublime.set_clipboard(html_content)
sublime.status_message('Exported result copied to clipboard')

def write_to_file(self, html_content, setting):
target_folder = setting.export_options['target_folder']

if target_folder is not None:
fullpath = self.view.file_name() or ''
timestamp_format = setting.export_options['timestamp_format']
timestr = time.strftime(timestamp_format, time.localtime())

if (not os.path.exists(fullpath) and target_folder == ".") or \
not os.path.isdir(target_folder):
target_folder = None
elif target_folder == '.':
fn_base, _ = os.path.splitext(fullpath)
html_fn = '%s%s.html' % (fn_base, timestr)
elif not os.path.exists(fullpath):
html_fn = os.path.join(target_folder, 'Untitled%s.html' % timestr)
else:
fn_base = os.path.basename(fullpath)
html_fn = os.path.join(target_folder, '%s%s.html' % (fn_base, timestr))

# No target folder, create file in temporary directory
if target_folder is None:
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as f:
html_fn = f.name

with codecs.open(html_fn, 'w', encoding='utf-8') as html_file:
html_file.write(html_content)

return html_fn

def run(self, edit, clipboard_only=False):
view = self.view
try:
html_content = RendererManager.render_view_to_string(view)
setting = Setting.instance()
target_folder = setting.export_options['target_folder']

if target_folder is not None:
fullpath = self.view.file_name() or ''
timestamp_format = setting.export_options['timestamp_format']
timestr = time.strftime(timestamp_format, time.localtime())

if (not os.path.exists(fullpath) and target_folder == ".") or \
not os.path.isdir(target_folder):
target_folder = None
elif target_folder == '.':
fn_base, _ = os.path.splitext(fullpath)
html_fn = '%s%s.html' % (fn_base, timestr)
elif not os.path.exists(fullpath):
html_fn = os.path.join(target_folder, 'Untitled%s.html' % timestr)
else:
fn_base = os.path.basename(fullpath)
html_fn = os.path.join(target_folder, '%s%s.html' % (fn_base, timestr))

if target_folder is None:
with tempfile.NamedTemporaryFile(delete=False, suffix='.html') as f:
html_fn = f.name
if clipboard_only:
self.copy_to_clipboard(html_content)
return

with codecs.open(html_fn, 'w', encoding='utf-8') as html_file:
html_file.write(html_content)
setting = Setting.instance()
html_fn = self.write_to_file(html_content, setting)

# Copy contents to clipboard
if setting.export_options['copy_to_clipboard']:
sublime.set_clipboard(html_content)
sublime.status_message('Exported result copied to clipboard')
self.copy_to_clipboard(html_content)

# Open output file if necessary
if setting.export_options['open_after_exporting']:
log.info('Launching web browser for %s', html_fn)
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,22 @@ The default key bindings:

**Windows, Linux:**

* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>O</kbd>: Preview current file.
* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>X</kbd>: Export to html file.
* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>O</kbd>: Preview current Markup file.
* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>X</kbd>: Export Markup to html file.
* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>C</kbd>: Copy Markup to clipboard.

**OSX:**

* <kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>O</kbd>: Preview current file.
* <kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>X</kbd>: Export to html file.
* <kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>O</kbd>: Preview current Markup file.
* <kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>X</kbd>: Export Markup to html file.
* <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>C</kbd>: Copy Markup to clipboard.

### Command Palette

Available OmniMarkupPreviewer commands in the command palette:

* `OmniMarkupPreviewer: Preview Current File`
* `OmniMarkupPreviewer: Export to html File`
* `OmniMarkupPreviewer: Sweep Cache (Remove Unused)`
* `OmniMarkupPreviewer: Empty Cache`

Known Issues
Expand Down

0 comments on commit 29f445d

Please sign in to comment.