<s> import os <EOL> import sublime <EOL> import sublime_plugin <EOL> import tempfile <EOL> import webbrowser <EOL> import json <EOL> import subprocess <EOL> use_curl = False <EOL> try : <EOL> import ssl <EOL> except ImportError as e : <EOL> use_curl = True <EOL> try : <EOL> import urllib2 <EOL> except ImportError : <EOL> import urllib . request <EOL> html = """<STR_LIT>""" <EOL> def call_exe ( command , dir ) : <EOL> process = subprocess . Popen ( <EOL> command , <EOL> cwd = dir , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , <EOL> universal_newlines = True ) <EOL> stdout , stderr = process . communicate ( ) <EOL> exit_code = process . wait ( ) <EOL> if exit_code : <EOL> raise Exception ( stdout ) <EOL> return stdout <EOL> def generate_preview ( text ) : <EOL> if int ( sublime . version ( ) ) > <NUM_LIT> : <EOL> return html . replace ( "<STR_LIT>" , text . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) ) <EOL> return html . replace ( "<STR_LIT>" , text . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:ignore>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) ) <EOL> class markdown_html_preview_command ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> try : <EOL> selection = sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) <EOL> html = generate_preview ( self . view . substr ( selection ) ) <EOL> temp_file = tempfile . NamedTemporaryFile ( delete = False , suffix = '<STR_LIT>' ) <EOL> if int ( sublime . version ( ) ) > <NUM_LIT> : <EOL> temp_file . write ( bytes ( html , '<STR_LIT>' ) ) <EOL> else : <EOL> temp_file . write ( html ) <EOL> temp_file . close ( ) <EOL> webbrowser . open ( "<STR_LIT>" + temp_file . name ) <EOL> except Exception as e : <EOL> sublime . error_message ( "<STR_LIT>" + str ( e ) ) </s>
