Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit ae6616a

Browse files
committed
Merge branch 'dev'
2 parents 83f3d79 + 9fd4295 commit ae6616a

File tree

7 files changed

+222
-46
lines changed

7 files changed

+222
-46
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#### Features
88
- Snippet tag functionalities
99
- Edit option for snippets
10-
- IMEX module *(Export option)*
10+
- IMEX module: *(Import & Export)*
11+
- CSV ~ *Simple spreadsheet format* `importable`
1112
#### Improvements
1213
- Quick Start Guide enhanced
1314
- `README.md`
15+
- IMEX module configuration
1416
#### Patches
1517
- Debug output
1618
- Available debug commands reduced

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Happy Coding :)
1515
## Reference
1616

1717
#### Commands
18-
There are basically three commands snippet-injector ships with:
18+
The following commands are registered by Snippet Injector and can be accessed via the command palette.
19+
If stated, the commands can also be called through menus or via hotkey.
1920

2021
##### **snippet-injector:create**
2122
This command creates a new snippet from the current selection in the current editor.
@@ -27,6 +28,7 @@ You will be prompted for a snippet title.
2728
*Predefined Hotkey:*
2829
> "Ctrl + Alt + O"
2930
31+
---
3032

3133
##### **snippet-injector:update**
3234
This command updates an existing snippet's content to the current selection in the current editor.
@@ -35,6 +37,7 @@ You will be prompted for choosing an existing snippet.
3537
*Name in menus:*
3638
> "Update existing snippet"
3739
40+
---
3841

3942
##### **snippet-injector:insert**
4043
This command injects a snippet to the current marker position(s).
@@ -46,6 +49,7 @@ You will be prompted to choose a snippet from a list.
4649
*Predefined Hotkey:*
4750
> "Ctrl + Alt + I"
4851
52+
---
4953

5054
##### **snippet-injector:delete**
5155
This command deletes a snippet from the local storage.
@@ -54,6 +58,7 @@ You will be prompted for the snippet name to delete.
5458
*Name in menus:*
5559
> "Delete snippet"
5660
61+
---
5762

5863
##### **snippet-injector:toggledebug**
5964
This command toggles all debugging options for the package.

lib/db/imex.json

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
1-
{
2-
"export": {
3-
"csv": {
4-
"labels": [
5-
"Title",
6-
"Author",
7-
"Content",
8-
"Language",
9-
"Tags",
10-
"created with version"
11-
],
12-
"order": [
13-
"title",
14-
"author",
15-
"content",
16-
"lang",
17-
"tags",
18-
"version"
19-
],
20-
"divider": {
21-
"field": ";",
22-
"line": "\n"
1+
[
2+
{
3+
"name": "csv",
4+
"importable": true,
5+
"dialogOptions": {
6+
"name": "CSV File",
7+
"extensions": ["csv"]
8+
},
9+
"labels": [
10+
"Title",
11+
"Author",
12+
"Content",
13+
"Language",
14+
"Tags",
15+
"created with version"
16+
],
17+
"order": [
18+
"title",
19+
"author",
20+
"content",
21+
"lang",
22+
"tags",
23+
"version"
24+
],
25+
"convert": [
26+
{
27+
"type": "replace",
28+
"name": "content",
29+
"search": "\n",
30+
"replace": "[LF]"
31+
},
32+
{
33+
"type": "replace",
34+
"name": "content",
35+
"search": "\r",
36+
"replace": "[CR]"
37+
},
38+
{
39+
"type": "replace",
40+
"name": "content",
41+
"search": ";",
42+
"replace": "[SC]"
43+
},
44+
{
45+
"type": "json",
46+
"name": "tags"
2347
}
48+
],
49+
"divider": {
50+
"field": ";",
51+
"line": "\n"
2452
}
2553
}
26-
}
54+
]

lib/imex.js

Lines changed: 123 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,51 +41,101 @@ export default class IMEX {
4141
}
4242

4343
storage_import() {
44+
var filters = new Array();
45+
this.data.forEach(function(e) {
46+
if(e.importable) {
47+
filters.push({
48+
name: e.dialogOptions.name,
49+
extensions: e.dialogOptions.extensions
50+
});
51+
}
52+
});
53+
filters.push({name: 'Any File', extensions: ['*']});
54+
var _this = this;
4455

56+
dialog.showOpenDialog({
57+
buttonLabel: 'Import',
58+
properties: ['openFile'],
59+
filters: filters
60+
},function(filenames) {
61+
var filename = filenames[0];
62+
if(Util.isset(filename,'string')) {
63+
var temp = path.extname(filename).replace('.','').toLowerCase();
64+
switch(temp) {
65+
case 'csv':
66+
_this._impcsv(filename);
67+
return true;
68+
break;
69+
default:
70+
return false;
71+
}
72+
}
73+
});
4574
}
4675

4776
// internal methods
4877

4978
_expcsv() {
5079
var storage = this.store;
51-
var proto = this.data.export.csv;
80+
var config = this.data.filter(function(e) {
81+
if(e.name === 'csv') {
82+
return true;
83+
} else {
84+
return false;
85+
}
86+
})[0];
5287
var files = storage.retrieveFiles();
5388
var csv_string = '';
5489
if(files.length > 0) {
55-
proto.labels.forEach(function(element,index,array) {
90+
config.labels.forEach(function(element,index,array) {
5691
csv_string += element;
5792
if(index < array.length-1) {
58-
csv_string += proto.divider.field;
93+
csv_string += config.divider.field;
5994
} else {
60-
csv_string += proto.divider.line;
95+
csv_string += config.divider.line;
6196
}
6297
})
6398
files.forEach(function(element,index,array) {
6499
var snippet = new Snippet(JSON.parse(storage.retrieveFile(element.replace('.json',''))));
65-
proto.order.forEach(function(element,index,array) {
66-
if(element === 'content') {
67-
csv_string += '"'+snippet._get(element).split('\n').join('[LF]').split('\r').join('[CR]').split(';').join('[SC]')+'"';
68-
} else if(element === 'tags') {
69-
csv_string += '"'+JSON.stringify(snippet._get(element))+'"';
70-
} else {
71-
csv_string += '"'+snippet._get(element)+'"';
100+
config.order.forEach(function(e,i,a) {
101+
var insert = snippet._get(e);
102+
if(config.convert.length > 0) {
103+
config.convert.filter(function(item) {
104+
if(item.name === e) {
105+
return true;
106+
} else {
107+
return false;
108+
}
109+
}).forEach(function(conversion) {
110+
if(conversion.type === 'replace') {
111+
insert = insert.split(conversion.search).join(conversion.replace);
112+
} else if(conversion.type === 'json') {
113+
insert = JSON.stringify(insert);
114+
}
115+
});
72116
}
117+
csv_string += insert;
73118

74-
if(index < array.length-1) {
75-
csv_string += proto.divider.field;
119+
if(i < a.length-1) {
120+
csv_string += config.divider.field;
76121
} else {
77-
csv_string += proto.divider.line;
122+
csv_string += config.divider.line;
78123
}
79124
});
80125
});
81126

127+
var filters = new Array();
128+
this.data.forEach(function(e) {
129+
filters.push({
130+
name: e.dialogOptions.name,
131+
extensions: e.dialogOptions.extensions
132+
});
133+
});
134+
filters.push({name: 'Any File', extensions: ['*']});
135+
82136
dialog.showSaveDialog({
83-
title: 'Choose the location for your export',
84137
buttonLabel: 'Export',
85-
filters: [
86-
{name: 'CSV File', extensions: ['csv']},
87-
{name: 'Any File', extensions: ['*']}
88-
]
138+
filters: filters
89139
},function(filename) {
90140
if(Util.isset(filename,'string')) {
91141
fs.writeFileSync(filename,csv_string);
@@ -103,4 +153,58 @@ export default class IMEX {
103153
return false;
104154
}
105155
}
156+
157+
_impcsv(filename) {
158+
var storage = this.store;
159+
var config = this.data.filter(function(e) {
160+
if(e.name === 'csv') {
161+
return true;
162+
} else {
163+
return false;
164+
}
165+
})[0];
166+
167+
var count = 0;
168+
var file = fs.readFileSync(filename).toString();
169+
file.split(config.divider.line).forEach(function(e,i,a) {
170+
var temp = (e !== '' && e !== ' ');
171+
config.labels.forEach(function(label) {
172+
if(e.indexOf(label) > -1) {
173+
temp = false;
174+
}
175+
});
176+
if(temp) {
177+
var snippet = new Snippet();
178+
e.split(config.divider.field).forEach(function(field,index) {
179+
var type = config.order[index];
180+
var insert = field;
181+
if(config.convert.length > 0) {
182+
config.convert.filter(function(item) {
183+
if(item.name === type) {
184+
return true;
185+
} else {
186+
return false;
187+
}
188+
}).forEach(function(conversion) {
189+
if(conversion.type === 'replace') {
190+
insert = insert.split(conversion.replace).join(conversion.search);
191+
} else if(conversion.type === 'json') {
192+
insert = JSON.parse(insert);
193+
}
194+
});
195+
}
196+
snippet._set(type,insert);
197+
});
198+
storage.store(snippet);
199+
count++;
200+
}
201+
});
202+
if(count < 1) {
203+
atom.notifications.addWarning('No snippets have been imported.', null)
204+
} else if(count == 1) {
205+
atom.notifications.addSuccess('One snippet has been imported.', null);
206+
} else {
207+
atom.notifications.addSuccess(count+' snippets have been imported.', null);
208+
}
209+
}
106210
}

lib/snippet-injector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export default {
5555
}));
5656
// IMEX commmands
5757
this.subscriptions.add(atom.commands.add('atom-workspace', {
58-
'snippet-injector:export-to-csv': function() {_this.imex_module.storage_export('csv')}
58+
'snippet-injector:export-to-csv': function() {_this.imex_module.storage_export('csv')},
59+
'snippet-injector:import': function() {_this.imex_module.storage_import()}
5960
}));
6061

6162
if(window.localStorage.getItem('snippet-injector-debug') === 'true') {

lib/snippet.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,38 @@ export default class Snippet {
154154
}
155155
}
156156

157+
_set(attribute,value) {
158+
if(Util.isset(attribute,'string')) {
159+
switch (attribute.toLowerCase()) {
160+
case 'content':
161+
this.setContent(value);
162+
break;
163+
case 'title':
164+
this.setTitle(value);
165+
break;
166+
case 'tags':
167+
this.setTags(value);
168+
break;
169+
case 'lang':
170+
this.setLang(value);
171+
break;
172+
case 'uid':
173+
this.setUID(value);
174+
break;
175+
case 'version':
176+
this.setVersion(value);
177+
break;
178+
case 'author':
179+
this.setAuthor(value);
180+
break;
181+
default:
182+
return undefined;
183+
}
184+
} else {
185+
return undefined;
186+
}
187+
}
188+
157189
static getLastUpdate() {
158190
return lastUpdate;
159191
}

menus/snippet-injector.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@
5252
"type": "separator"
5353
},
5454
{
55-
"label": "Export",
55+
"label": "Export Snippets",
5656
"submenu": [
5757
{
5858
"label": "CSV",
5959
"command": "snippet-injector:export-to-csv"
6060
}
6161
]
62+
},
63+
{
64+
"label": "Import Snippets",
65+
"command": "snippet-injector:import"
6266
}
6367
]
6468
}

0 commit comments

Comments
 (0)