Module: FlexibleInclude::FlexiblePrivateMethods
- Includes:
- FromToUntil
- Included in:
- FlexibleInclude, FlexibleInclude
- Defined in:
- lib/flexible_include_private_methods.rb
Constant Summary collapse
- PREFIX =
"<button class='copyBtn' data-clipboard-target=".freeze
- SUFFIX =
"title='Copy to clipboard'><img src='/assets/images/clippy.svg' alt='Copy to clipboard' style='width: 13px'></button>".freeze
Instance Method Summary collapse
- #denied(msg) ⇒ Object
- #highlight(content, pattern) ⇒ Object
- #maybe_raise_error(msg, throw_error: true) ⇒ Object
- #parse_args ⇒ Object
- #remove_html_tags(string) ⇒ Object
- #render_completion ⇒ Object
- #run(cmd) ⇒ Object
- #setup ⇒ Object
- #wrap_in_pre(path, content) ⇒ Object
Instance Method Details
#denied(msg) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/flexible_include_private_methods.rb', line 11 def denied(msg) msg_no_html = (msg) @logger.error { "#{@page['path']} - #{msg_no_html}" } raise FlexibleIncludeError, "#{@page['path']} - #{msg_no_html}".red, [] if @die_on_path_denied "<p class='flexible_error'>#{msg}</p>" end |
#highlight(content, pattern) ⇒ Object
19 20 21 22 23 |
# File 'lib/flexible_include_private_methods.rb', line 19 def highlight(content, pattern) raise FlexibleIncludeError, "content is a #{content.class}, not a String" unless content.instance_of? String content.gsub(Regexp.new(pattern), "<span class='bg_yellow_nopad'>\\0</span>") end |
#maybe_raise_error(msg, throw_error: true) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/flexible_include_private_methods.rb', line 25 def maybe_raise_error(msg, throw_error: true) @logger.error { msg } return "<span class='flexible_error'>#{msg}</span>" unless throw_error raise FlexibleIncludeError, msg, [] end |
#parse_args ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/flexible_include_private_methods.rb', line 32 def parse_args = @helper.parameter_specified? 'copyButton' @dark = ' dark' if @helper.parameter_specified? 'dark' @do_not_escape = @helper.parameter_specified? 'do_not_escape' @download = @helper.parameter_specified? 'download' @from = @helper.parameter_specified? 'from' @highlight_pattern = @helper.parameter_specified? 'highlight' @label = @helper.parameter_specified? 'label' @label_specified = @label @number_lines = @helper.parameter_specified? 'number' @strip = @helper.parameter_specified? 'strip' @to = @helper.parameter_specified? 'to' @until = @helper.parameter_specified? 'until' # Download, dark, label or number implies pre @pre = @helper.parameter_specified?('pre') || || @dark || @download || @label_specified || @number_lines @filename = @helper.parameter_specified? 'file' unless @filename # Do this after all other options have been checked for @filename = @helper.params.first.first @helper.delete_parameter @helper.params.first end raise StandardError, "@filename (#{@filename}) is not a string", [] unless @filename.instance_of? String @label ||= @filename @logger.debug("@filename=#{@filename}") end |
#remove_html_tags(string) ⇒ Object
62 63 64 |
# File 'lib/flexible_include_private_methods.rb', line 62 def (string) string.gsub(/<[^>]*>/, '') end |
#render_completion ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/flexible_include_private_methods.rb', line 66 def render_completion unless @path.start_with?('!') unless File.exist?(@path) && Dir.exist?(File.dirname(@path)) return maybe_raise_error( "#{@path} does not exist, referenced on line #{@line_number} (after front matter) of #{@page['path']}", throw_error: @die_on_file_error ) end unless Pathname.new(@path).readable? return maybe_raise_error( "#{@path} is not readable, referenced on line #{@line_number} (after front matter) of #{@page['path']}", throw_error: @die_on_file_error ) end @contents = File.read @path unless @contents.instance_of? String maybe_raise_error("contents has type #{@contents.class}, not a String", throw_error: @die_on_file_error) end end @contents = FromToUntil.from(@contents, @from) if @from @contents = FromToUntil.to(@contents, @to) if @to @contents = FromToUntil.until(@contents, @until) if @until contents2 = @do_not_escape ? @contents : FlexibleClassMethods.escape_html(@contents) contents2.strip! if @strip maybe_raise_error("contents2 is a #{contents2.class}, not a String", throw_error: @die_on_file_error) unless contents2.instance_of? String contents2 = highlight(contents2, @highlight_pattern) if @highlight_pattern contents2 = FlexibleInclude.number_content(contents2) if @number_lines result = @pre ? wrap_in_pre(@path, contents2) : contents2 " \#{result}\n \#{@helper.attribute if @helper.attribution}\n END_OUTPUT\nend\n" |
#run(cmd) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/flexible_include_private_methods.rb', line 105 def run(cmd) if cmd.empty? @do_not_escape = true return maybe_raise_error('FlexibleIncludeError: Empty command string', throw_error: @die_on_other_error) end @logger.debug { "Executing #{cmd}" } %x[#{cmd}].chomp rescue FlexibleIncludeError => e @logger.error { e. } exit! 1 if @die_on_any_flexible_include_error e. rescue StandardError => e # Assumes the wasa the result of running a command; this might not be a valid assumption. Awaiting a bug report. @do_not_escape = true e = e.exception "'#{e.message}' while executing '#{cmd}'" maybe_reraise_error(e, throw_error: @die_on_run_error) end |
#setup ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/flexible_include_private_methods.rb', line 124 def setup @helper.gem_file __FILE__ # Enables attribution self.class.security_check config = @config[PLUGIN_NAME] if config @die_on_file_error = config['die_on_file_error'] == true @die_on_other_error = config['die_on_other_error'] == true @die_on_path_denied = config['die_on_path_denied'] == true @die_on_run_error = config['die_on_run_error'] == true @die_on_any_flexible_include_error = @die_on_file_error || @die_on_other_error || @die_on_path_denied || @die_on_run_error end parse_args end |
#wrap_in_pre(path, content) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/flexible_include_private_methods.rb', line 143 def wrap_in_pre(path, content) basename = File.basename(path) label_or_href = if @download label = @label_specified ? @label : basename " <a href='data:text/plain;charset=UTF-8,\#{basename}' download='\#{basename}'\n title='Click on the file name to download the file'>\#{label}</a>\n END_HREF\n else\n @label_specified ? @label : basename\n end\n pre_id = \"id\#{SecureRandom.hex 6}\"\n copy_button = @copy_button ? \"\#{PREFIX}'#\#{pre_id}'\#{SUFFIX}\" : ''\n dark_label = ' darkLabel' if @dark\n <<~END_PRE\n <div class=\"codeLabel\#{dark_label}\">\#{label_or_href}</div>\n <pre data-lt-active=\"false\" class=\"pre_tag maxOneScreenHigh copyContainer\#{@dark}\" id=\"\#{pre_id}\">\#{copy_button}\#{content}</pre>\n END_PRE\nend\n" |