8
8
# Hsiaoming Yang, published at https://github.com/lepture/mistune
9
9
# You may have to install the dependencies from github. Please read the README file.
10
10
11
- import gi , os , glob , configobj , mistune
11
+ import sys , os , gi , glob , configobj , mistune
12
12
gi .require_version ('Gtk' , '3.0' )
13
- gi .require_version ('WebKit ' , '3 .0' )
13
+ gi .require_version ('WebKit2 ' , '4 .0' )
14
14
gi .require_version ('Keybinder' , '3.0' )
15
- from gi .repository import Gtk , Gdk , WebKit , Gio , GObject , Keybinder
15
+ from gi .repository import Gtk , Gdk , WebKit2 , Gio , GObject , Keybinder
16
16
from mistune .directives import DirectiveToc
17
17
from mistune .directives import Admonition
18
18
19
19
#Define a version number to be able to change it just in one place
20
20
global VERSION_NUMBER , WEB_PAGE
21
- VERSION_NUMBER = "1.0b "
21
+ VERSION_NUMBER = "1.1 "
22
22
WEB_PAGE = "https://github.com/dsancheznet/pymd/"
23
23
24
24
@@ -56,11 +56,12 @@ class MainWindow(Gtk.Window):
56
56
This class defines the main window of the application
57
57
"""
58
58
59
- def __init__ (self ):
59
+ def __init__ ( self , tmpDefaultFile = "" ):
60
60
#Instantiate config file parser
61
61
self .cConfig = configobj .ConfigObj ( os .path .expanduser ("~" )+ '/.config/pymd/default.config' )
62
62
Keybinder .init ()
63
63
Keybinder .bind ("<Ctrl>R" , self .onUpdateButtonClicked )
64
+ Keybinder .bind ("<Ctrl>P" , self .onPrintButtonClicked )
64
65
#TODO: Error checking
65
66
#Define a path, where the css files a loaded from
66
67
self .myCSSPath = os .path .expanduser ("~" )+ '/.config/pymd/css/' #TODO: Error checking
@@ -78,7 +79,7 @@ def __init__(self):
78
79
self .cHeaderBar = Gtk .HeaderBar ()
79
80
self .cHeaderBar .set_show_close_button (True )
80
81
self .cHeaderBar .props .title = "pymd " + VERSION_NUMBER
81
- self .cHeaderBar .props .subtitle = ""
82
+ self .cHeaderBar .props .subtitle = tmpDefaultFile
82
83
self .set_titlebar (self .cHeaderBar )
83
84
84
85
#Buttons for Headerbar
@@ -97,10 +98,16 @@ def __init__(self):
97
98
myUpdateButton .add ( Gtk .Image .new_from_gicon ( Gio .ThemedIcon ( name = "emblem-synchronizing-symbolic" ), Gtk .IconSize .BUTTON ) )
98
99
myUpdateButton .connect ( "clicked" , self .onUpdateButtonClicked )
99
100
101
+ myPrintButton = Gtk .Button ()
102
+ myPrintButton .props .relief = Gtk .ReliefStyle .NONE
103
+ myPrintButton .add ( Gtk .Image .new_from_gicon ( Gio .ThemedIcon ( name = "printer-printing" ), Gtk .IconSize .BUTTON ) )
104
+ myPrintButton .connect ( "clicked" , self .onPrintButtonClicked )
105
+
100
106
#Pack everything
101
107
self .cHeaderBar .pack_start ( myConfigButton )
102
108
self .cHeaderBar .pack_end ( myOpenButton )
103
109
self .cHeaderBar .pack_end ( myUpdateButton )
110
+ self .cHeaderBar .pack_start ( myPrintButton )
104
111
#----------Header Bar ***END***
105
112
106
113
#----------Scroller
@@ -110,10 +117,11 @@ def __init__(self):
110
117
self .myScroller .set_policy ( Gtk .PolicyType .AUTOMATIC , Gtk .PolicyType .AUTOMATIC )
111
118
112
119
#Create a webview
113
- self .myWebView = WebKit .WebView ()
120
+ self .myWebView = WebKit2 .WebView ()
114
121
self .myWebView .get_settings ().allow_universal_access_from_file_urls = True ;
115
- self .myWebView .load_string ( '<html><body><br><center>Empty...</center></body></html>' , "text/html" , "UTF-8" , "file://" )
116
- self .myWebView .props .settings .props .enable_default_context_menu = False
122
+ self .myWebView .connect ('context-menu' , self .contextMenuCallback )
123
+ self .myWebView .load_html ('<html><body><br><center>Empty...</center></body></html>' , "file://" )
124
+ #self.myWebView.props.settings.props.enable_default_context_menu = False
117
125
self .myScroller .add ( self .myWebView )
118
126
self .add ( self .myScroller )
119
127
#----------Scroller ***END***
@@ -145,6 +153,11 @@ def __init__(self):
145
153
self .connect ( "delete-event" , self .mainWindowDelete )
146
154
#Connect the destroy signal to the main loop quit function
147
155
self .connect ("destroy" , Gtk .main_quit )
156
+ #Load the data (it won't do anythign if subtitle is empty)
157
+ self .loadFileData ( self .cHeaderBar .props .subtitle )
158
+
159
+ def contextMenuCallback ( self , web_view , context_menu , event , hit_test_result ):
160
+ return True
148
161
149
162
def mainWindowDelete ( self , widget , data = None ):
150
163
#Save default styelsheet
@@ -165,26 +178,6 @@ def onConfigButtonClicked( self, widget ):
165
178
#Do your thing
166
179
self .cPopover .popup ()
167
180
168
- def loadFileData ( self , tmpFilename ):
169
- #DEBUG: print('Reloading...')
170
- #Create a mistune instance with all plugins enabled and a custom renderer
171
- tmpMarkdown = mistune .create_markdown ( escape = False , renderer = ImagePathRenderer ( os .path .dirname ( tmpFilename )+ "/" ), plugins = [Admonition (), DirectiveToc (), 'url' , 'strikethrough' , 'footnotes' , 'table' ],)
172
- #Open the file for reading // TODO: Error checking -> Filie must be readable
173
- tmpMarkDownFile = open ( tmpFilename , "r" )
174
- #Load the contents of the file as raw data
175
- tmpRawMarkdown = tmpMarkDownFile .read ()
176
- #Define a string to prepend
177
- tmpPreHTML = '<html><head><link rel="stylesheet" type="text/css" href="file://' + self .myCSSPath + self .myCSSFile .get_active_text () + '"></head><body class="markdown-body">'
178
- #Define a string to append
179
- tmpPostHTML = "</body></html>"
180
- #Define a base uri // HINT: Without this, we are getting errors trying to load local files
181
- tmpBaseURI = "file://"
182
- #Load the data into the webview.
183
- self .myWebView .load_string ( tmpPreHTML + tmpMarkdown ( tmpRawMarkdown ) + tmpPostHTML , "text/html" , "UTF-8" , tmpBaseURI )
184
- #Close the file we opened for reading
185
- tmpMarkDownFile .close ()
186
- #DEBUG: print(tmpPreHTML + tmpMarkdown( tmpRawMarkdown ) + tmpPostHTML)
187
-
188
181
def onUpdateButtonClicked ( self , widget ):
189
182
#Do we have a file loaded?
190
183
if self .cHeaderBar .props .subtitle != "" :
@@ -224,15 +217,58 @@ def onOpenButtonClicked( self, widget ):
224
217
#Destroy de dialog
225
218
tmpDialog .destroy ()
226
219
220
+ def onPrintButtonClicked ( self , widget ):
221
+ #Do we have a file open?
222
+ if self .cHeaderBar .props .subtitle != "" :
223
+ #YES - Launch print dialog
224
+ tmpOperation = WebKit2 .PrintOperation .new ( self .myWebView )
225
+ if tmpOperation .run_dialog () == WebKit2 .PrintOperationResponse .CANCEL :
226
+ print ("Print Cancelled!" )
227
+ else :
228
+ tmpDialog = Gtk .MessageDialog (self , 0 , Gtk .MessageType .INFO , Gtk .ButtonsType .OK , "Print Job" )
229
+ tmpDialog .format_secondary_text ( "Your print job has been sent to the printer..." )
230
+ tmpDialog .run ()
231
+ tmpDialog .destroy ()
232
+
233
+ def loadFileData ( self , tmpFilename ):
234
+ #Does the file we are trying to load exist and is actually a file?
235
+ if os .path .exists ( tmpFilename ) and os .path .isfile ( tmpFilename ):
236
+ #YES - Create a mistune instance with all plugins enabled and a custom renderer
237
+ tmpMarkdown = mistune .create_markdown ( escape = False , renderer = ImagePathRenderer ( os .path .dirname ( tmpFilename )+ "/" ), plugins = [Admonition (), DirectiveToc (), 'url' , 'strikethrough' , 'footnotes' , 'table' ],)
238
+ #Open the file for reading // TODO: Error checking -> Filie must be readable
239
+ tmpMarkDownFile = open ( tmpFilename , "r" )
240
+ #Load the contents of the file as raw data
241
+ tmpRawMarkdown = tmpMarkDownFile .read ()
242
+ #Define a string to prepend
243
+ tmpPreHTML = '<html><head><link rel="stylesheet" type="text/css" href="file://' + self .myCSSPath + self .myCSSFile .get_active_text () + '"></head><body class="markdown-body">'
244
+ #Define a string to append
245
+ tmpPostHTML = "</body></html>"
246
+ #Define a base uri // HINT: Without this, we are getting errors trying to load local files
247
+ tmpBaseURI = "file://"
248
+ #Load the data into the webview.
249
+ self .myWebView .load_html ( tmpPreHTML + tmpMarkdown ( tmpRawMarkdown ) + tmpPostHTML , tmpBaseURI )
250
+ #Close the file we opened for reading
251
+ tmpMarkDownFile .close ()
252
+ #DEBUG: print(tmpPreHTML + tmpMarkdown( tmpRawMarkdown ) + tmpPostHTML)
253
+ else :
254
+ print ("File not found..." ) #TODO: Load an adecuate HTML into the view.
255
+
256
+ if __name__ == "__main__" :
227
257
228
- #Create Main Window
229
- myWindow = MainWindow ()
258
+ #Check for parameters
259
+ if len ( sys .argv )> 1 :
260
+ #DEBUG: print( "Arguments: " + str(sys.argv) )
261
+ #Create Main Window with parameter
262
+ myWindow = MainWindow ( sys .argv [1 ] )
263
+ else :
264
+ #Create Main Window without parameter
265
+ myWindow = MainWindow ()
230
266
231
- #Connect the destroy signal to the main loop quit function
232
- myWindow .connect ("destroy" , Gtk .main_quit )
267
+ #Connect the destroy signal to the main loop quit function
268
+ myWindow .connect ("destroy" , Gtk .main_quit )
233
269
234
- #Show Windows on screen
235
- myWindow .show_all ()
270
+ #Show Windows on screen
271
+ myWindow .show_all ()
236
272
237
- #Main Loop
238
- Gtk .main ()
273
+ #Main Loop
274
+ Gtk .main ()
0 commit comments