Module: SnippetGenerator
- Included in:
- SnippetCli::Commands::New
- Defined in:
- lib/snippet_generator.rb
Overview
Some Definitions for the sake of readability.
Constant Summary collapse
- NEW_LINE =
"\n"
- QUOTE =
'"'
Instance Method Summary collapse
-
#heading_snippet_export(file_to_write, heading) ⇒ Object
New a YAML Comment to separate sections of snippet file.
-
#initialize_espanso_yml(file_to_write) ⇒ Object
Just writes matches: at the beginning of file so espanso can read the mapping.
-
#input_form_snippet_export(file_to_write, form_trigger, form_statement) ⇒ Object
Any input fields should be entered with double brackets around them when passed in as form_statement For example “AJ likes coding in {language} and using {editor} to write code.”.
-
#picklist_snippet_export(form_trigger, statement, form_fields, formvalues, file_to_write) ⇒ Object
! TO DO: REFACTOR FORM METHODS INTO ONE METHOD which accounts for all cases.
-
#single_snippet_export(file_to_write, trigger, replacement) ⇒ Object
Writes a snippet to file when given trigger and replacement strings.
-
#textarea_snippet_export(file_to_write) ⇒ Object
News a snippet with large text box.
Instance Method Details
#heading_snippet_export(file_to_write, heading) ⇒ Object
New a YAML Comment to separate sections of snippet file.
29 30 31 |
# File 'lib/snippet_generator.rb', line 29 def heading_snippet_export(file_to_write,heading) File.open(file_to_write,"a") { |file| file.write("# "+ heading+NEW_LINE) } end |
#initialize_espanso_yml(file_to_write) ⇒ Object
Just writes matches: at the beginning of file so espanso can read the mapping.
8 9 10 |
# File 'lib/snippet_generator.rb', line 8 def initialize_espanso_yml(file_to_write) File.open(file_to_write,"a") { |file| file.write('matches:'+NEW_LINE) } end |
#input_form_snippet_export(file_to_write, form_trigger, form_statement) ⇒ Object
Any input fields should be entered with double brackets around them when passed in as form_statement For example “AJ likes coding in {language} and using {editor} to write code.”
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/snippet_generator.rb', line 36 def input_form_snippet_export(file_to_write, form_trigger,form_statement) File.open(file_to_write,"a") { |file| file.write(' - trigger: '+QUOTE+':'+form_trigger+QUOTE+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' form: |-'+NEW_LINE)} if (form_statement.instance_of?(String)) then File.open(file_to_write,"a") { |file| file.write(' '+form_statement)} else form_statement.each do |item| File.open(file_to_write,"a") { |file| file.write(' '+item) } end end File.open(file_to_write,"a") { |file| file.write(NEW_LINE) } end |
#picklist_snippet_export(form_trigger, statement, form_fields, formvalues, file_to_write) ⇒ Object
! TO DO: REFACTOR FORM METHODS INTO ONE METHOD which accounts for all cases. Add comments clarifying ! DATA STRUCTURE NEEDED. Takes a string for trigger. form_values should be an array.form_fields should also be of type array. Parses statements and news picklists based on form fields and values for each field provided
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/snippet_generator.rb', line 53 def picklist_snippet_export(form_trigger,statement,form_fields,formvalues,file_to_write) form_fields.each do |value| value+':' end form_type = 'choice' File.open(file_to_write,"a") { |file| file.write(' - trigger: '+'":'+form_trigger+QUOTE+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' form: '+QUOTE+statement+QUOTE+NEW_LINE) } form_fields.each do |value| File.open(file_to_write,"a") { |file| file.write(' form_fields:'+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' '+form_fields+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' type: '+ form_type+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' values:'+NEW_LINE) } formvalues.each do |value| File.open(file_to_write,"a") { |file| file.write(' - '+QUOTE+value+QUOTE+NEW_LINE) } end end end |
#single_snippet_export(file_to_write, trigger, replacement) ⇒ Object
Writes a snippet to file when given trigger and replacement strings.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/snippet_generator.rb', line 14 def single_snippet_export(file_to_write,trigger,replacement) File.open(file_to_write,"a") { |file| file.write(NEW_LINE+' - trigger: '+'":'+trigger+QUOTE+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' replace: |-'+NEW_LINE) } if (replacement.instance_of?(String)) then File.open(file_to_write,"a") { |file| file.write(' '+replacement) } else replacement.each do |item| File.open(file_to_write,"a") { |file| file.write(' '+item) } end end File.open(file_to_write,"a") { |file| file.write(NEW_LINE) } end |
#textarea_snippet_export(file_to_write) ⇒ Object
News a snippet with large text box
74 75 76 77 78 79 80 |
# File 'lib/snippet_generator.rb', line 74 def textarea_snippet_export(file_to_write) File.open(file_to_write,"a") { |file| file.write(' - trigger: '+QUOTE+':'+form_trigger+QUOTE+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' form: |'+NEW_LINE)} File.open(file_to_write,"a") { |file| file.write(' '+form_statement+NEW_LINE)} File.open(file_to_write,"a") { |file| file.write(' '+field_names+NEW_LINE) } File.open(file_to_write,"a") { |file| file.write(' '+"multiline: true"+NEW_LINE) } end |