Module: FlexibleInclude::FlexiblePrivateMethods
- 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
9 10 11 12 13 14 15 |
# File 'lib/flexible_include_private_methods.rb', line 9 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
17 18 19 20 21 |
# File 'lib/flexible_include_private_methods.rb', line 17 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'>\\0</span>") end |
#maybe_raise_error(msg, throw_error: true) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/flexible_include_private_methods.rb', line 23 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
30 31 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 |
# File 'lib/flexible_include_private_methods.rb', line 30 def parse_args @copy_button = @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') || @copy_button || @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
60 61 62 |
# File 'lib/flexible_include_private_methods.rb', line 60 def (string) string.gsub(/<[^>]*>/, '') end |
#render_completion ⇒ Object
64 65 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 |
# File 'lib/flexible_include_private_methods.rb', line 64 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 <<~END_OUTPUT #{result} #{@helper.attribute if @helper.attribution} END_OUTPUT end |
#run(cmd) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/flexible_include_private_methods.rb', line 103 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.}' while executing '#{cmd}'" maybe_reraise_error(e, throw_error: @die_on_run_error) end |
#setup ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/flexible_include_private_methods.rb', line 122 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/flexible_include_private_methods.rb', line 141 def wrap_in_pre(path, content) basename = File.basename(path) label_or_href = if @download label = @label_specified ? @label : basename <<~END_HREF <a href='data:text/plain;charset=UTF-8,#{basename}' download='#{basename}' title='Click on the file name to download the file'>#{label}</a> END_HREF else @label_specified ? @label : basename end pre_id = "id#{SecureRandom.hex 6}" = @copy_button ? "#{PREFIX}'##{pre_id}'#{SUFFIX}" : '' dark_label = ' darkLabel' if @dark <<~END_PRE <div class="codeLabel#{dark_label}">#{label_or_href}</div> <pre data-lt-active="false" class="pre_tag maxOneScreenHigh copyContainer#{@dark}" id="#{pre_id}">#{}#{content}</pre> END_PRE end |