|
| 1 | +import sublime_plugin |
| 2 | +import sublime |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +class ConcatCommand(sublime_plugin.TextCommand): |
| 7 | + def run(self, edit): |
| 8 | + view = self.view |
| 9 | + window = view.window() |
| 10 | + region = sublime.Region(0, view.size()) |
| 11 | + |
| 12 | + path = '/'.join(self.view.file_name().split('/')[0:-1]) |
| 13 | + full_name = self.view.file_name().split('/')[-1] |
| 14 | + extension = full_name.split('.')[-1] |
| 15 | + file_name = '.'.join(full_name.split('.')[0:-1]) |
| 16 | + |
| 17 | + test = '/{0,2}@import url\(\'(.+)\'\);' |
| 18 | + i = self.view.find(test, 0) |
| 19 | + if i: |
| 20 | + new_view = window.new_file() |
| 21 | + new_view.set_name(file_name + '.cat.' + extension) |
| 22 | + region = sublime.Region(0, view.size()) |
| 23 | + content = view.substr(region) |
| 24 | + |
| 25 | + try: |
| 26 | + syntax_file = self.view.settings().get('syntax') |
| 27 | + new_view.set_syntax_file(syntax_file) |
| 28 | + except KeyError: |
| 29 | + print 'no syntax' |
| 30 | + pass |
| 31 | + |
| 32 | + edit = new_view.begin_edit('cat') |
| 33 | + new_view.insert(edit, 0, content) |
| 34 | + |
| 35 | + while new_view.find(test, 0): |
| 36 | + i = new_view.find(test, 0) |
| 37 | + content = new_view.substr(i) |
| 38 | + m = re.search(test, content) |
| 39 | + |
| 40 | + if m: |
| 41 | + included = m.group(1) |
| 42 | + |
| 43 | + try: |
| 44 | + f = open(path + '/' + included) |
| 45 | + file_content = f.read() |
| 46 | + new_view.replace(edit, i, file_content) |
| 47 | + f.close() |
| 48 | + except IOError: |
| 49 | + print 'cannot open', included |
| 50 | + raise |
| 51 | + |
| 52 | + new_view.end_edit(edit) |
| 53 | + window.run_command("save") |
| 54 | + window.run_command("build") |
0 commit comments