serbea 0.9.0 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/serbea/bridgetown_support.rb +3 -9
- data/lib/serbea/helpers.rb +6 -27
- data/lib/serbea/rails_support.rb +19 -0
- data/lib/serbea/template_engine.rb +58 -46
- data/lib/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 212a077a67f1553dc07fc8dd1b989647e19803953fba03976ec9ef95e08a2321
         | 
| 4 | 
            +
              data.tar.gz: feda65d983102a217bd8426a8404712f882f23d2c6c3fa4be1981518fb20aae4
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 94edc63cbeb21ce38db5ab1270c2b06f5479d5fd529ea12ccca68d69d6abe7fece8dc83cbfeeeba8e724b4fa7c165f831b23d6059bccd9aef022bc6d6545f16d
         | 
| 7 | 
            +
              data.tar.gz: b10964bcc171ff4903ae5b98dea1db73b0f21f95a1bbb02027681a84e0834c7838ad3901e00a740586844fc4bf5be78d5d00fc5710e0c310f2bacf44026956db
         | 
| @@ -2,8 +2,10 @@ require "serbea/rouge_lexer" | |
| 2 2 | 
             
            require "bridgetown-core"
         | 
| 3 3 |  | 
| 4 4 | 
             
            module Bridgetown
         | 
| 5 | 
            -
              class SerbeaView <  | 
| 5 | 
            +
              class SerbeaView < ERBView
         | 
| 6 | 
            +
                alias_method :_erb_capture, :capture
         | 
| 6 7 | 
             
                include Serbea::Helpers
         | 
| 8 | 
            +
                alias_method :capture, :_erb_capture
         | 
| 7 9 |  | 
| 8 10 | 
             
                def partial(partial_name, options = {}, &block)
         | 
| 9 11 | 
             
                  options.merge!(options[:locals]) if options[:locals]
         | 
| @@ -17,14 +19,6 @@ module Bridgetown | |
| 17 19 | 
             
                    site.in_source_dir(site.config[:partials_dir], "#{partial_name}.serb")
         | 
| 18 20 | 
             
                  ).render(self, options)
         | 
| 19 21 | 
             
                end
         | 
| 20 | 
            -
             | 
| 21 | 
            -
                def markdownify(input = nil, &block)
         | 
| 22 | 
            -
                  content = Bridgetown::Utils.reindent_for_markdown(
         | 
| 23 | 
            -
                    block.nil? ? input.to_s : capture(&block)
         | 
| 24 | 
            -
                  )
         | 
| 25 | 
            -
                  converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
         | 
| 26 | 
            -
                  converter.convert(content).strip
         | 
| 27 | 
            -
                end
         | 
| 28 22 | 
             
              end
         | 
| 29 23 |  | 
| 30 24 | 
             
              module Converters
         | 
    
        data/lib/serbea/helpers.rb
    CHANGED
    
    | @@ -6,52 +6,31 @@ module Serbea | |
| 6 6 | 
             
                  Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
         | 
| 7 7 | 
             
                end
         | 
| 8 8 |  | 
| 9 | 
            -
                def capture( | 
| 9 | 
            +
                def capture(*args)
         | 
| 10 10 | 
             
                  previous_buffer_state = @_erbout
         | 
| 11 11 | 
             
                  @_erbout = Serbea::OutputBuffer.new
         | 
| 12 | 
            -
             | 
| 13 | 
            -
                  # For compatibility with ActionView, not used by Bridgetown normally
         | 
| 14 | 
            -
                  previous_ob_state = @output_buffer
         | 
| 15 | 
            -
                  @output_buffer = Serbea::OutputBuffer.new
         | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
                  result = instance_exec(obj, &block)
         | 
| 19 | 
            -
                  if @output_buffer != ""
         | 
| 20 | 
            -
                    # use Rails' ActionView buffer if present
         | 
| 21 | 
            -
                    result = @output_buffer
         | 
| 22 | 
            -
                  end
         | 
| 12 | 
            +
                  result = yield(*args)
         | 
| 23 13 | 
             
                  @_erbout = previous_buffer_state
         | 
| 24 | 
            -
                  @output_buffer = previous_ob_state
         | 
| 25 14 |  | 
| 26 15 | 
             
                  result&.html_safe
         | 
| 27 16 | 
             
                end
         | 
| 28 | 
            -
             | 
| 17 | 
            +
             | 
| 29 18 | 
             
                def pipeline(context, value)
         | 
| 30 19 | 
             
                  Pipeline.new(context, value)
         | 
| 31 20 | 
             
                end
         | 
| 32 | 
            -
             | 
| 21 | 
            +
             | 
| 33 22 | 
             
                def helper(name, &helper_block)
         | 
| 34 23 | 
             
                  self.class.define_method(name) do |*args, &block|
         | 
| 35 24 | 
             
                    previous_buffer_state = @_erbout
         | 
| 36 25 | 
             
                    @_erbout = Serbea::OutputBuffer.new
         | 
| 37 | 
            -
              
         | 
| 38 | 
            -
                    # For compatibility with ActionView, not used by Bridgetown normally
         | 
| 39 | 
            -
                    previous_ob_state = @output_buffer
         | 
| 40 | 
            -
                    @output_buffer = Serbea::OutputBuffer.new
         | 
| 41 | 
            -
              
         | 
| 42 26 | 
             
                    result = helper_block.call(*args, &block)
         | 
| 43 | 
            -
                    if @output_buffer != ""
         | 
| 44 | 
            -
                      # use Rails' ActionView buffer if present
         | 
| 45 | 
            -
                      result = @output_buffer
         | 
| 46 | 
            -
                    end
         | 
| 47 27 | 
             
                    @_erbout = previous_buffer_state
         | 
| 48 | 
            -
             | 
| 49 | 
            -
              
         | 
| 28 | 
            +
             | 
| 50 29 | 
             
                    result.is_a?(String) ? result.html_safe : result
         | 
| 51 30 | 
             
                  end
         | 
| 52 31 | 
             
                end
         | 
| 53 32 | 
             
                alias_method :macro, :helper
         | 
| 54 | 
            -
             | 
| 33 | 
            +
             | 
| 55 34 | 
             
                def h(input)
         | 
| 56 35 | 
             
                  ERB::Util.h(input.to_s)
         | 
| 57 36 | 
             
                end
         | 
    
        data/lib/serbea/rails_support.rb
    CHANGED
    
    | @@ -37,4 +37,23 @@ module Serbea | |
| 37 37 | 
             
              end
         | 
| 38 38 | 
             
            end
         | 
| 39 39 |  | 
| 40 | 
            +
            Serbea::TemplateEngine.directive :form, ->(code, buffer) do
         | 
| 41 | 
            +
              buffer << "{%= form_with model:"
         | 
| 42 | 
            +
              buffer << code
         | 
| 43 | 
            +
              buffer << " %}"
         | 
| 44 | 
            +
            end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            Serbea::TemplateEngine.directive :_, ->(code, buffer) do
         | 
| 47 | 
            +
              tag_name, space, params = code.lstrip.partition(%r(\s)m)
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              if tag_name.end_with?(":")
         | 
| 50 | 
            +
                tag_name.chomp!(":")
         | 
| 51 | 
            +
                tag_name = ":#{tag_name}" unless tag_name.start_with?(":")
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              buffer << "{%= tag.tag_string "
         | 
| 55 | 
            +
              buffer << tag_name << ", " << params
         | 
| 56 | 
            +
              buffer << " %}"
         | 
| 57 | 
            +
            end
         | 
| 58 | 
            +
             | 
| 40 59 | 
             
            ActionView::Template.register_template_handler(:serb, Serbea::Rails::TemplateHandler)
         | 
| @@ -14,40 +14,58 @@ module Serbea | |
| 14 14 | 
             
              class TemplateEngine < Erubi::Engine
         | 
| 15 15 | 
             
                FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
         | 
| 16 16 |  | 
| 17 | 
            -
                def self. | 
| 18 | 
            -
                   | 
| 17 | 
            +
                def self.directive(new_directive, directive_resolution)
         | 
| 18 | 
            +
                  directives[new_directive.to_s] = directive_resolution
         | 
| 19 19 | 
             
                end
         | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 20 | 
            +
             | 
| 21 | 
            +
                def self.directives
         | 
| 22 | 
            +
                  @directives ||= {
         | 
| 23 | 
            +
                    "@" => ->(code, buffer) do
         | 
| 24 | 
            +
                      pieces = code.split(" ")
         | 
| 25 | 
            +
                      if pieces[0].start_with?(/[A-Z]/) # Ruby class name
         | 
| 26 | 
            +
                        pieces[0].prepend " "
         | 
| 27 | 
            +
                        pieces[0] << ".new("
         | 
| 28 | 
            +
                      else # string or something else
         | 
| 29 | 
            +
                        pieces[0].prepend "("
         | 
| 30 | 
            +
                      end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                      includes_block = false
         | 
| 33 | 
            +
                      pieces.reverse.each do |piece|
         | 
| 34 | 
            +
                        if piece == "do" && (pieces.last == "do" || pieces.last.end_with?("|"))
         | 
| 35 | 
            +
                          piece.prepend(") ")
         | 
| 36 | 
            +
                          includes_block = true
         | 
| 37 | 
            +
                          break
         | 
| 38 | 
            +
                        end
         | 
| 39 | 
            +
                      end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                      if includes_block
         | 
| 42 | 
            +
                        buffer << "{%= render#{pieces.join(" ")} %}"
         | 
| 43 | 
            +
                      else
         | 
| 44 | 
            +
                        pieces.last << ")"
         | 
| 45 | 
            +
                        buffer << "{%= render#{pieces.join(" ")} %}"
         | 
| 46 | 
            +
                      end
         | 
| 47 | 
            +
                    end
         | 
| 48 | 
            +
                  }
         | 
| 22 49 | 
             
                end
         | 
| 23 50 |  | 
| 24 51 | 
             
                def self.front_matter_preamble=(varname)
         | 
| 25 52 | 
             
                  @front_matter_preamble = varname
         | 
| 26 53 | 
             
                end
         | 
| 54 | 
            +
             | 
| 27 55 | 
             
                def self.front_matter_preamble
         | 
| 28 56 | 
             
                  @front_matter_preamble ||= "frontmatter = YAML.load"
         | 
| 29 57 | 
             
                end
         | 
| 30 | 
            -
             | 
| 58 | 
            +
             | 
| 31 59 | 
             
                def self.has_yaml_header?(template)
         | 
| 32 60 | 
             
                  template.lines.first&.match? %r!\A---\s*\r?\n!
         | 
| 33 61 | 
             
                end
         | 
| 34 | 
            -
             | 
| 62 | 
            +
             | 
| 35 63 | 
             
                def initialize(input, properties={})
         | 
| 36 64 | 
             
                  properties[:regexp] = /{%(={1,2}|-|\#|%)?(.*?)([-=])?%}([ \t]*\r?\n)?/m
         | 
| 37 65 | 
             
                  properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter)
         | 
| 38 66 | 
             
                  super process_serbea_input(input, properties), properties
         | 
| 39 67 | 
             
                end
         | 
| 40 | 
            -
             | 
| 41 | 
            -
                def add_postamble(postamble)
         | 
| 42 | 
            -
                  src << postamble
         | 
| 43 | 
            -
                  src << "#{@bufvar}.html_safe"
         | 
| 44 | 
            -
              
         | 
| 45 | 
            -
                  src.gsub!("__RAW_START_PRINT__", "{{")
         | 
| 46 | 
            -
                  src.gsub!("__RAW_END_PRINT__", "}}")
         | 
| 47 | 
            -
                  src.gsub!("__RAW_START_EVAL__", "{%")
         | 
| 48 | 
            -
                  src.gsub!("__RAW_END_EVAL__", "%}")
         | 
| 49 | 
            -
                end
         | 
| 50 | 
            -
              
         | 
| 68 | 
            +
             | 
| 51 69 | 
             
                def process_serbea_input(template, properties)
         | 
| 52 70 | 
             
                  buff = ""
         | 
| 53 71 |  | 
| @@ -149,39 +167,23 @@ module Serbea | |
| 149 167 | 
             
                  string = buff
         | 
| 150 168 | 
             
                  buff = ""
         | 
| 151 169 | 
             
                  until string.empty?
         | 
| 152 | 
            -
                    text, code, string = string.partition(/{%@(.*?)%}/m)
         | 
| 153 | 
            -
             | 
| 170 | 
            +
                    text, code, string = string.partition(/{%@([a-z_]+)?(.*?)%}/m)
         | 
| 171 | 
            +
             | 
| 154 172 | 
             
                    buff << text
         | 
| 155 173 | 
             
                    if code.length > 0
         | 
| 156 | 
            -
                       | 
| 174 | 
            +
                      directive = $1
         | 
| 175 | 
            +
                      code = $2
         | 
| 157 176 | 
             
                      unless ["end", ""].include? code.strip
         | 
| 158 | 
            -
                         | 
| 159 | 
            -
             | 
| 160 | 
            -
                        pieces = code.split(" ")
         | 
| 161 | 
            -
                        if pieces[0].start_with?(/[A-Z]/) # Ruby class name
         | 
| 162 | 
            -
                          pieces[0].prepend " "
         | 
| 163 | 
            -
                          pieces[0] << ".new("
         | 
| 164 | 
            -
                        else # string or something else
         | 
| 165 | 
            -
                          pieces[0].prepend "("
         | 
| 166 | 
            -
                        end
         | 
| 177 | 
            +
                        directive = $1 ? self.class.directives[$1] : self.class.directives["@"]
         | 
| 167 178 |  | 
| 168 | 
            -
                         | 
| 169 | 
            -
             | 
| 170 | 
            -
                           | 
| 171 | 
            -
             | 
| 172 | 
            -
                             | 
| 173 | 
            -
                            break
         | 
| 179 | 
            +
                        if directive
         | 
| 180 | 
            +
                          additional_length = "#{buff}#{code}".lines.size
         | 
| 181 | 
            +
                          directive.(code, buff)
         | 
| 182 | 
            +
                          (additional_length - buff.lines.size).times do
         | 
| 183 | 
            +
                            buff << "{% %}\n" # preserve original directive line length
         | 
| 174 184 | 
             
                          end
         | 
| 175 | 
            -
                        end
         | 
| 176 | 
            -
             | 
| 177 | 
            -
                        if includes_block
         | 
| 178 | 
            -
                          buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
         | 
| 179 185 | 
             
                        else
         | 
| 180 | 
            -
                           | 
| 181 | 
            -
                          buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
         | 
| 182 | 
            -
                        end
         | 
| 183 | 
            -
                        (original_line_length - 1).times do
         | 
| 184 | 
            -
                          buff << "\n{% %}" # preserve original directive line length
         | 
| 186 | 
            +
                          raise "Handler for Serbea template directive `#{$1}' not found"
         | 
| 185 187 | 
             
                        end
         | 
| 186 188 | 
             
                      else
         | 
| 187 189 | 
             
                        buff << "{% end %}"
         | 
| @@ -191,7 +193,7 @@ module Serbea | |
| 191 193 |  | 
| 192 194 | 
             
                  buff
         | 
| 193 195 | 
             
                end
         | 
| 194 | 
            -
             | 
| 196 | 
            +
             | 
| 195 197 | 
             
                private
         | 
| 196 198 |  | 
| 197 199 | 
             
                def add_code(code)
         | 
| @@ -215,5 +217,15 @@ module Serbea | |
| 215 217 | 
             
                def add_expression_result_escaped(code)
         | 
| 216 218 | 
             
                  add_expression_result(code)
         | 
| 217 219 | 
             
                end
         | 
| 218 | 
            -
             | 
| 220 | 
            +
             | 
| 221 | 
            +
                def add_postamble(postamble)
         | 
| 222 | 
            +
                  src << postamble
         | 
| 223 | 
            +
                  src << "#{@bufvar}.html_safe"
         | 
| 224 | 
            +
              
         | 
| 225 | 
            +
                  src.gsub!("__RAW_START_PRINT__", "{{")
         | 
| 226 | 
            +
                  src.gsub!("__RAW_END_PRINT__", "}}")
         | 
| 227 | 
            +
                  src.gsub!("__RAW_START_EVAL__", "{%")
         | 
| 228 | 
            +
                  src.gsub!("__RAW_END_EVAL__", "%}")
         | 
| 229 | 
            +
                end
         | 
| 230 | 
            +
              end
         | 
| 219 231 | 
             
            end
         | 
    
        data/lib/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: serbea
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.10.4
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Bridgetown Team
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2020- | 
| 11 | 
            +
            date: 2020-12-05 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activesupport
         |