Skip to content
Newer
Older
100644 755 lines (629 sloc) 21.2 KB
1b5aa30 @fmarcia Initial commit
authored
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Middleware layer that communicates between editor and Zen Coding.
6 This layer describes all available Zen Coding actions, like
7 "Expand Abbreviation".
8 @author Sergey Chikuyonok (serge.che@gmail.com)
9 @link http://chikuyonok.ru
10 """
11 from zencoding import zen_core as zen_coding
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
12 from zencoding import html_matcher, zen_file
13 from zen_core import char_at, ZenError
14 import re
15 import base64
16
17 mime_types = {
18 'gif': 'image/gif',
19 'png': 'image/png',
20 'jpg': 'image/jpeg',
21 'jpeg': 'image/jpeg',
22 'svg': 'image/svg+xml',
23 'html': 'text/html',
24 'htm': 'text/html'
25 }
1b5aa30 @fmarcia Initial commit
authored
26
27 def find_abbreviation(editor):
28 """
29 Search for abbreviation in editor from current caret position
30 @param editor: Editor instance
31 @type editor: ZenEditor
32 @return: str
33 """
34 start, end = editor.get_selection_range()
35 if start != end:
36 # abbreviation is selected by user
37 return editor.get_content()[start:end]
38
39 # search for new abbreviation from current caret position
40 cur_line_start, cur_line_end = editor.get_current_line_range()
41 return zen_coding.extract_abbreviation(editor.get_content()[cur_line_start:start])
42
43 def expand_abbreviation(editor, syntax=None, profile_name=None):
44 """
45 Find from current caret position and expand abbreviation in editor
46 @param editor: Editor instance
47 @type editor: ZenEditor
48 @param syntax: Syntax type (html, css, etc.)
49 @type syntax: str
50 @param profile_name: Output profile name (html, xml, xhtml)
51 @type profile_name: str
52 @return: True if abbreviation was expanded successfully
53 """
54 if syntax is None: syntax = editor.get_syntax()
55 if profile_name is None: profile_name = editor.get_profile_name()
56
57 range_start, caret_pos = editor.get_selection_range()
58 abbr = find_abbreviation(editor)
59 content = ''
60
61 if abbr:
62 content = zen_coding.expand_abbreviation(abbr, syntax, profile_name)
63 if content:
64 editor.replace_content(content, caret_pos - len(abbr), caret_pos)
65 return True
66
67 return False
68
69 def expand_abbreviation_with_tab(editor, syntax, profile_name='xhtml'):
70 """
71 A special version of <code>expandAbbreviation</code> function: if it can't
72 find abbreviation, it will place Tab character at caret position
73 @param editor: Editor instance
74 @type editor: ZenEditor
75 @param syntax: Syntax type (html, css, etc.)
76 @type syntax: str
77 @param profile_name: Output profile name (html, xml, xhtml)
78 @type profile_name: str
79 """
80 if not expand_abbreviation(editor, syntax, profile_name):
81 editor.replace_content(zen_coding.get_variable('indentation'), editor.get_caret_pos())
82
83 return True
84
85 def match_pair(editor, direction='out', syntax=None):
86 """
87 Find and select HTML tag pair
88 @param editor: Editor instance
89 @type editor: ZenEditor
90 @param direction: Direction of pair matching: 'in' or 'out'.
91 @type direction: str
92 """
93 direction = direction.lower()
94 if syntax is None: syntax = editor.get_profile_name()
95
96 range_start, range_end = editor.get_selection_range()
97 cursor = range_end
98 content = editor.get_content()
99 rng = None
100
101 old_open_tag = html_matcher.last_match['opening_tag']
102 old_close_tag = html_matcher.last_match['closing_tag']
103
104 if direction == 'in' and old_open_tag and range_start != range_end:
105 # user has previously selected tag and wants to move inward
106 if not old_close_tag:
107 # unary tag was selected, can't move inward
108 return False
109 elif old_open_tag.start == range_start:
110 if content[old_open_tag.end] == '<':
111 # test if the first inward tag matches the entire parent tag's content
112 _r = html_matcher.find(content, old_open_tag.end + 1, syntax)
113 if _r[0] == old_open_tag.end and _r[1] == old_close_tag.start:
114 rng = html_matcher.match(content, old_open_tag.end + 1, syntax)
115 else:
116 rng = (old_open_tag.end, old_close_tag.start)
117 else:
118 rng = (old_open_tag.end, old_close_tag.start)
119 else:
120 new_cursor = content[0:old_close_tag.start].find('<', old_open_tag.end)
121 search_pos = new_cursor + 1 if new_cursor != -1 else old_open_tag.end
122 rng = html_matcher.match(content, search_pos, syntax)
123 else:
124 rng = html_matcher.match(content, cursor, syntax)
125
126 if rng and rng[0] is not None:
127 editor.create_selection(rng[0], rng[1])
128 return True
129 else:
130 return False
131
132 def match_pair_inward(editor):
133 return match_pair(editor, 'in')
134
135 def match_pair_outward(editor):
136 return match_pair(editor, 'out')
137
138 def narrow_to_non_space(text, start, end):
139 """
140 Narrow down text indexes, adjusting selection to non-space characters
141 @type text: str
142 @type start: int
143 @type end: int
144 @return: list
145 """
146 # narrow down selection until first non-space character
147 while start < end:
148 if not text[start].isspace():
149 break
150
151 start += 1
152
153 while end > start:
154 end -= 1
155 if not text[end].isspace():
156 end += 1
157 break
158
159 return start, end
160
161 def wrap_with_abbreviation(editor, abbr, syntax=None, profile_name=None):
162 """
163 Wraps content with abbreviation
164 @param editor: Editor instance
165 @type editor: ZenEditor
166 @param syntax: Syntax type (html, css, etc.)
167 @type syntax: str
168 @param profile_name: Output profile name (html, xml, xhtml)
169 @type profile_name: str
170 """
171 if not abbr: return None
172
173 if syntax is None: syntax = editor.get_syntax()
174 if profile_name is None: profile_name = editor.get_profile_name()
175
176 start_offset, end_offset = editor.get_selection_range()
177 content = editor.get_content()
178
179 if start_offset == end_offset:
180 # no selection, find tag pair
181 rng = html_matcher.match(content, start_offset, profile_name)
182
183 if rng[0] is None: # nothing to wrap
184 return None
185 else:
186 start_offset, end_offset = rng
187
188 start_offset, end_offset = narrow_to_non_space(content, start_offset, end_offset)
189 line_bounds = get_line_bounds(content, start_offset)
190 padding = get_line_padding(content[line_bounds[0]:line_bounds[1]])
191
192 new_content = content[start_offset:end_offset]
193 result = zen_coding.wrap_with_abbreviation(abbr, unindent_text(new_content, padding), syntax, profile_name)
194
195 if result:
196 editor.replace_content(result, start_offset, end_offset)
197 return True
198
199 return False
200
201 def unindent(editor, text):
202 """
203 Unindent content, thus preparing text for tag wrapping
204 @param editor: Editor instance
205 @type editor: ZenEditor
206 @param text: str
207 @return str
208 """
209 return unindent_text(text, get_current_line_padding(editor))
210
211 def unindent_text(text, pad):
212 """
213 Removes padding at the beginning of each text's line
214 @type text: str
215 @type pad: str
216 """
217 lines = zen_coding.split_by_lines(text)
218
219 for i,line in enumerate(lines):
220 if line.startswith(pad):
221 lines[i] = line[len(pad):]
222
223 return zen_coding.get_newline().join(lines)
224
225 def get_current_line_padding(editor):
226 """
227 Returns padding of current editor's line
228 @return str
229 """
230 return get_line_padding(editor.get_current_line())
231
232 def get_line_padding(line):
233 """
234 Returns padding of current editor's line
235 @return str
236 """
237 m = re.match(r'^(\s+)', line)
238 return m and m.group(0) or ''
239
240 def find_new_edit_point(editor, inc=1, offset=0):
241 """
242 Search for new caret insertion point
243 @param editor: Editor instance
244 @type editor: ZenEditor
245 @param inc: Search increment: -1 — search left, 1 — search right
246 @param offset: Initial offset relative to current caret position
247 @return: -1 if insertion point wasn't found
248 """
249 cur_point = editor.get_caret_pos() + offset
250 content = editor.get_content()
251 max_len = len(content)
252 next_point = -1
253 re_empty_line = r'^\s+$'
254
255 def get_line(ix):
256 start = ix
257 while start >= 0:
258 c = content[start]
259 if c == '\n' or c == '\r': break
260 start -= 1
261
262 return content[start:ix]
263
264 while cur_point < max_len and cur_point > 0:
265 cur_point += inc
266 cur_char = char_at(content, cur_point)
267 next_char = char_at(content, cur_point + 1)
268 prev_char = char_at(content, cur_point - 1)
269
270 if cur_char in '"\'':
271 if next_char == cur_char and prev_char == '=':
272 # empty attribute
273 next_point = cur_point + 1
274 elif cur_char == '>' and next_char == '<':
275 # between tags
276 next_point = cur_point + 1
277 elif cur_char in '\r\n':
278 # empty line
279 if re.search(re_empty_line, get_line(cur_point - 1)):
280 next_point = cur_point
281
282 if next_point != -1: break
283
284 return next_point
285
286 def prev_edit_point(editor):
287 """
288 Move caret to previous edit point
289 @param editor: Editor instance
290 @type editor: ZenEditor
291 """
292 cur_pos = editor.get_caret_pos()
293 new_point = find_new_edit_point(editor, -1)
294
295 if new_point == cur_pos:
296 # we're still in the same point, try searching from the other place
297 new_point = find_new_edit_point(editor, -1, -2)
298
299 if new_point != -1:
300 editor.set_caret_pos(new_point)
301 return True
302
303 return False
304
305 def next_edit_point(editor):
306 """
307 Move caret to next edit point
308 @param editor: Editor instance
309 @type editor: ZenEditor
310 """
311 new_point = find_new_edit_point(editor, 1)
312 if new_point != -1:
313 editor.set_caret_pos(new_point)
314 return True
315
316 return False
317
318 def insert_formatted_newline(editor, mode='html'):
319 """
320 Inserts newline character with proper indentation
321 @param editor: Editor instance
322 @type editor: ZenEditor
323 @param mode: Syntax mode (only 'html' is implemented)
324 @type mode: str
325 """
326 caret_pos = editor.get_caret_pos()
327 nl = zen_coding.get_newline()
328 pad = zen_coding.get_variable('indentation')
329
330 if mode == 'html':
331 # let's see if we're breaking newly created tag
332 pair = html_matcher.get_tags(editor.get_content(), editor.get_caret_pos(), editor.get_profile_name())
333
334 if pair[0] and pair[1] and pair[0]['type'] == 'tag' and pair[0]['end'] == caret_pos and pair[1]['start'] == caret_pos:
335 editor.replace_content(nl + pad + zen_coding.get_caret_placeholder() + nl, caret_pos)
336 else:
337 editor.replace_content(nl, caret_pos)
338 else:
339 editor.replace_content(nl, caret_pos)
340
341 return True
342
343 def select_line(editor):
344 """
345 Select line under cursor
346 @param editor: Editor instance
347 @type editor: ZenEditor
348 """
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
349 start, end = editor.get_current_line_range()
1b5aa30 @fmarcia Initial commit
authored
350 editor.create_selection(start, end)
351 return True
352
353 def go_to_matching_pair(editor):
354 """
355 Moves caret to matching opening or closing tag
356 @param editor: Editor instance
357 @type editor: ZenEditor
358 """
359 content = editor.get_content()
360 caret_pos = editor.get_caret_pos()
361
362 if content[caret_pos] == '<':
363 # looks like caret is outside of tag pair
364 caret_pos += 1
365
366 tags = html_matcher.get_tags(content, caret_pos, editor.get_profile_name())
367
368 if tags and tags[0]:
369 # match found
370 open_tag, close_tag = tags
371
372 if close_tag: # exclude unary tags
373 if open_tag['start'] <= caret_pos and open_tag['end'] >= caret_pos:
374 editor.set_caret_pos(close_tag['start'])
375 elif close_tag['start'] <= caret_pos and close_tag['end'] >= caret_pos:
376 editor.set_caret_pos(open_tag['start'])
377
378 return True
379
380 return False
381
382
383 def merge_lines(editor):
384 """
385 Merge lines spanned by user selection. If there's no selection, tries to find
386 matching tags and use them as selection
387 @param editor: Editor instance
388 @type editor: ZenEditor
389 """
390 start, end = editor.get_selection_range()
391 if start == end:
392 # find matching tag
393 pair = html_matcher.match(editor.get_content(), editor.get_caret_pos(), editor.get_profile_name())
394 if pair and pair[0] is not None:
395 start, end = pair
396
397 if start != end:
398 # got range, merge lines
399 text = editor.get_content()[start:end]
400 lines = map(lambda s: re.sub(r'^\s+', '', s), zen_coding.split_by_lines(text))
401 text = re.sub(r'\s{2,}', ' ', ''.join(lines))
402 editor.replace_content(text, start, end)
403 editor.create_selection(start, start + len(text))
404 return True
405
406 return False
407
408 def toggle_comment(editor):
409 """
410 Toggle comment on current editor's selection or HTML tag/CSS rule
411 @type editor: ZenEditor
412 """
413 syntax = editor.get_syntax()
414 if syntax == 'css':
415 return toggle_css_comment(editor)
416 else:
417 return toggle_html_comment(editor)
418
419 def toggle_html_comment(editor):
420 """
421 Toggle HTML comment on current selection or tag
422 @type editor: ZenEditor
423 @return: True if comment was toggled
424 """
425 start, end = editor.get_selection_range()
426 content = editor.get_content()
427
428 if start == end:
429 # no selection, find matching tag
430 pair = html_matcher.get_tags(content, editor.get_caret_pos(), editor.get_profile_name())
431 if pair and pair[0]: # found pair
432 start = pair[0].start
433 end = pair[1] and pair[1].end or pair[0].end
434
435 return generic_comment_toggle(editor, '<!--', '-->', start, end)
436
437 def toggle_css_comment(editor):
438 """
439 Simple CSS commenting
440 @type editor: ZenEditor
441 @return: True if comment was toggled
442 """
443 start, end = editor.get_selection_range()
444
445 if start == end:
446 # no selection, get current line
447 start, end = editor.get_current_line_range()
448
449 # adjust start index till first non-space character
450 start, end = narrow_to_non_space(editor.get_content(), start, end)
451
452 return generic_comment_toggle(editor, '/*', '*/', start, end)
453
454 def search_comment(text, pos, start_token, end_token):
455 """
456 Search for nearest comment in <code>str</code>, starting from index <code>from</code>
457 @param text: Where to search
458 @type text: str
459 @param pos: Search start index
460 @type pos: int
461 @param start_token: Comment start string
462 @type start_token: str
463 @param end_token: Comment end string
464 @type end_token: str
465 @return: None if comment wasn't found, list otherwise
466 """
467 start_ch = start_token[0]
468 end_ch = end_token[0]
469 comment_start = -1
470 comment_end = -1
471
472 def has_match(tx, start):
473 return text[start:start + len(tx)] == tx
474
475
476 # search for comment start
477 while pos:
478 pos -= 1
479 if text[pos] == start_ch and has_match(start_token, pos):
480 comment_start = pos
481 break
482
483 if comment_start != -1:
484 # search for comment end
485 pos = comment_start
486 content_len = len(text)
487 while content_len >= pos:
488 pos += 1
489 if text[pos] == end_ch and has_match(end_token, pos):
490 comment_end = pos + len(end_token)
491 break
492
493 if comment_start != -1 and comment_end != -1:
494 return comment_start, comment_end
495 else:
496 return None
497
498 def generic_comment_toggle(editor, comment_start, comment_end, range_start, range_end):
499 """
500 Generic comment toggling routine
501 @type editor: ZenEditor
502 @param comment_start: Comment start token
503 @type comment_start: str
504 @param comment_end: Comment end token
505 @type comment_end: str
506 @param range_start: Start selection range
507 @type range_start: int
508 @param range_end: End selection range
509 @type range_end: int
510 @return: bool
511 """
512 content = editor.get_content()
513 caret_pos = [editor.get_caret_pos()]
514 new_content = None
515
516 def adjust_caret_pos(m):
517 caret_pos[0] -= len(m.group(0))
518 return ''
519
520 def remove_comment(text):
521 """
522 Remove comment markers from string
523 @param {Sting} str
524 @return {String}
525 """
526 text = re.sub(r'^' + re.escape(comment_start) + r'\s*', adjust_caret_pos, text)
527 return re.sub(r'\s*' + re.escape(comment_end) + '$', '', text)
528
529 def has_match(tx, start):
530 return content[start:start + len(tx)] == tx
531
532 # first, we need to make sure that this substring is not inside comment
533 comment_range = search_comment(content, caret_pos[0], comment_start, comment_end)
534
535 if comment_range and comment_range[0] <= range_start and comment_range[1] >= range_end:
536 # we're inside comment, remove it
537 range_start, range_end = comment_range
538 new_content = remove_comment(content[range_start:range_end])
539 else:
540 # should add comment
541 # make sure that there's no comment inside selection
542 new_content = '%s %s %s' % (comment_start, re.sub(re.escape(comment_start) + r'\s*|\s*' + re.escape(comment_end), '', content[range_start:range_end]), comment_end)
543
544 # adjust caret position
545 caret_pos[0] += len(comment_start) + 1
546
547 # replace editor content
548 if new_content is not None:
549 d = caret_pos[0] - range_start
550 new_content = new_content[0:d] + zen_coding.get_caret_placeholder() + new_content[d:]
551 editor.replace_content(unindent(editor, new_content), range_start, range_end)
552 return True
553
554 return False
555
556 def split_join_tag(editor, profile_name=None):
557 """
558 Splits or joins tag, e.g. transforms it into a short notation and vice versa:
559 <div></div> → <div /> : join
560 <div /> → <div></div> : split
561 @param editor: Editor instance
562 @type editor: ZenEditor
563 @param profile_name: Profile name
564 @type profile_name: str
565 """
566 caret_pos = editor.get_caret_pos()
567 profile = zen_coding.get_profile(profile_name or editor.get_profile_name())
568 caret = zen_coding.get_caret_placeholder()
569
570 # find tag at current position
571 pair = html_matcher.get_tags(editor.get_content(), caret_pos, profile_name or editor.get_profile_name())
572 if pair and pair[0]:
573 new_content = pair[0].full_tag
574
575 if pair[1]: # join tag
576 closing_slash = ''
577 if profile['self_closing_tag'] is True:
578 closing_slash = '/'
579 elif profile['self_closing_tag'] == 'xhtml':
580 closing_slash = ' /'
581
582 new_content = re.sub(r'\s*>$', closing_slash + '>', new_content)
583
584 # add caret placeholder
585 if len(new_content) + pair[0].start < caret_pos:
586 new_content += caret
587 else:
588 d = caret_pos - pair[0].start
589 new_content = new_content[0:d] + caret + new_content[d:]
590
591 editor.replace_content(new_content, pair[0].start, pair[1].end)
592 else: # split tag
593 nl = zen_coding.get_newline()
594 pad = zen_coding.get_variable('indentation')
595
596 # define tag content depending on profile
597 tag_content = profile['tag_nl'] is True and nl + pad + caret + nl or caret
598
599 new_content = '%s%s</%s>' % (re.sub(r'\s*\/>$', '>', new_content), tag_content, pair[0].name)
600 editor.replace_content(new_content, pair[0].start, pair[0].end)
601
602 return True
603 else:
604 return False
605
606
607 def get_line_bounds(text, pos):
608 """
609 Returns line bounds for specific character position
610 @type text: str
611 @param pos: Where to start searching
612 @type pos: int
613 @return: list
614 """
615 start = 0
616 end = len(text) - 1
617
618 # search left
619 for i in range(pos - 1, 0, -1):
620 if text[i] in '\n\r':
621 start = i + 1
622 break
623
624 # search right
625 for i in range(pos, len(text)):
626 if text[i] in '\n\r':
627 end = i
628 break
629
630 return start, end
631
632 def remove_tag(editor):
633 """
634 Gracefully removes tag under cursor
635 @type editor: ZenEditor
636 """
637 caret_pos = editor.get_caret_pos()
638 content = editor.get_content()
639
640 # search for tag
641 pair = html_matcher.get_tags(content, caret_pos, editor.get_profile_name())
642 if pair and pair[0]:
643 if not pair[1]:
644 # simply remove unary tag
645 editor.replace_content(zen_coding.get_caret_placeholder(), pair[0].start, pair[0].end)
646 else:
647 tag_content_range = narrow_to_non_space(content, pair[0].end, pair[1].start)
648 start_line_bounds = get_line_bounds(content, tag_content_range[0])
649 start_line_pad = get_line_padding(content[start_line_bounds[0]:start_line_bounds[1]])
650 tag_content = content[tag_content_range[0]:tag_content_range[1]]
651
652 tag_content = unindent_text(tag_content, start_line_pad)
653 editor.replace_content(zen_coding.get_caret_placeholder() + tag_content, pair[0].start, pair[1].end)
654
655 return True
656 else:
657 return False
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
658
659 def encode_decode_base64(editor):
660 """
661 Encodes/decodes image under cursor to/from base64
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
662 @type editor: ZenEditor
663 @since: 0.65
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
664 """
665 data = editor.get_selection()
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
666 caret_pos, not_used = editor.get_selection_range() # (FM) caret_pos must point to the start of the selection
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
667
668 if not data:
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
669 # no selection, try to find image bounds from current caret position
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
670 text = editor.get_content()
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
671
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
672 while caret_pos >= 0:
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
673 if text.startswith('src=', caret_pos): # found <img src="">, (FM) startswith already exists
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
674 m = re.match(r'^(src=(["\'])?)([^\'"<>\s]+)\1?', text[caret_pos:])
675 if m:
676 data = m.group(3)
677 caret_pos += len(m.group(1))
678 break
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
679 elif text.startswith('url(', caret_pos): # found CSS url() pattern, (FM) startswith already exists
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
680 m = re.match(r'^(url\(([\'"])?)([^\'"\)\s]+)\1?/', text[caret_pos:])
681 if m:
682 data = m.group(3)
683 caret_pos += len(m.group(1))
684 break
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
685
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
686 caret_pos -= 1
687
688 if data:
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
689 if data.startswith('data:'): # (FM) startswith already exists
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
690 return decode_from_base64(editor, data, caret_pos)
691 else:
692 return encode_to_base64(editor, data, caret_pos)
693 else:
694 return False
695
696 def encode_to_base64(editor, img_path, pos):
697 """
698 Encodes image to base64
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
699 @requires: zen_file
700
701 @type editor: ZenEditor
702 @param img_path: Path to image
703 @type img_path: str
704 @param pos: Caret position where image is located in the editor
705 @type pos: int
706 @return: bool
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
707 """
708 editor_file = editor.get_file_path()
709 default_mime_type = 'application/octet-stream'
710
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
711 if editor_file is None:
712 raise ZenError("You should save your file before using this action")
713
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
714
715 # locate real image path
716 real_img_path = zen_file.locate_file(editor_file, img_path)
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
717 if real_img_path is None:
718 raise ZenError("Can't find %s file" % img_path)
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
719
720 b64 = base64.b64encode(zen_file.read(real_img_path))
721 if not b64:
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
722 raise ZenError("Can't encode file content to base64")
723
724
725 b64 = 'data:' + (mime_types[zen_file.get_ext(real_img_path)] or default_mime_type) + ';base64,' + b64
726
727 editor.replace_content(b64, pos, pos + len(img_path)) # (FM) $0 isn't used in gedit
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
728 return True
729
730 def decode_from_base64(editor, data, pos):
731 """
732 Decodes base64 string back to file.
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
733 @requires: zen_editor.prompt
734 @requires: zen_file
735
736 @type editor: ZenEditor
737 @param data: Base64-encoded file content
738 @type data: str
739 @param pos: Caret position where image is located in the editor
740 @type pos: int
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
741 """
742 # ask user to enter path to file
743 file_path = editor.prompt('Enter path to file (absolute or relative)')
744 if not file_path:
745 return False
746
747 abs_path = zen_file.create_path(editor.get_file_path(), file_path)
748 if not abs_path:
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
749 raise ZenError("Can't save file")
5bc97fc @fmarcia Introduce 'Toggle between image url and data'
authored
750
01adefc @fmarcia Update from sergeche; explain changes in comments
authored
751
752 zen_file.save(abs_path, base64.b64decode( re.sub(r'^data\:.+?;.+?,', '', data) ))
753 editor.replace_content(file_path, pos, pos + len(data)) # (FM) $0 isn't used in gedit
754 return True
Something went wrong with that request. Please try again.