Class: Conductor::Filter

Inherits:
String
  • Object
show all
Defined in:
lib/conductor/filter.rb

Overview

String filtering

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#add_comment, #add_mmd, #add_yaml, #append, #append!, #autolink, #bool?, #bool_to_symbol, #clean_encode, #clean_encode!, #comment?, #count_h1s, #date?, #decrease_headers, #delete_meta, #delete_mmd, #delete_yaml, #ensure_h1, #ensure_h1!, #ensure_mmd_meta_newline, #find_file_in, #first_h1, #first_h2, #fix_headers, #fix_headers!, #fix_hierarchy, #increase_headers, #increase_headers!, #inject_after_meta, #insert_css, #insert_file, #insert_javascript, #insert_raw_javascript, #insert_script, #insert_stylesheet, #insert_title, #insert_toc, #meta?, #meta_insert_point, #meta_type, #normalize_filter, #normalize_headers, #normalize_headers!, #normalize_include_type, #normalize_position, #number?, #pandoc?, #read_title, #replace_all, #replace_one, #set_meta, #split_list, #strip_meta, #strip_time, #time?, #title_from_slug, #titleize, #to_bool, #to_date, #to_day, #to_pattern, #to_rx, #utf8, #utf8!, #wrap_style, #yaml?

Constructor Details

#initialize(filter) ⇒ Filter

Instantiate a filter

Parameters:

  • filter (Filter)

    The filter



672
673
674
675
# File 'lib/conductor/filter.rb', line 672

def initialize(filter)
  @filter, @params = filter.normalize_filter
  super
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



665
666
667
# File 'lib/conductor/filter.rb', line 665

def filter
  @filter
end

#paramsObject (readonly)

Returns the value of attribute params.



665
666
667
# File 'lib/conductor/filter.rb', line 665

def params
  @params
end

Instance Method Details

#processString

Process STDIN with @filter

Returns:



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/conductor/filter.rb', line 682

def process
  content = Conductor.stdin

  case @filter
  when /(insert|add|inject)stylesheet/
    @params.each do |sheet|
      content = content.insert_stylesheet(sheet)
    end
    content
  when /(insert|add|inject)(css|style)/
    @params.each do |css|
      content = content.insert_css(css)
    end
    content
  when /(insert|add|inject)title/
    amt = 0
    if @params
      amt = if @params[0] =~ /^[yts]/
              1
            else
              @params[0].to_i
            end
    end
    content.insert_title(shift: amt)
  when /(insert|add|inject)script/
    content.append!("\n\n<div>")
    @params.each do |script|
      content = content.insert_script(script)
    end
    "#{content}</div>"
  when /(prepend|append|insert|inject)(raw|file|code)/
    m = Regexp.last_match

    position = if @params.count == 2
                 @params[1].normalize_position
               else
                 m[1].normalize_position
               end
    content.insert_file(@params[0], m[2].normalize_include_type, position)
  when /inserttoc/
    max = @params.nil? || @params.empty? ? nil : @params[0]

    after = if @params && @params.count == 2
              @params[1] =~ /2/ ? :h2 : :h1
            else
              :start
            end

    content.insert_toc(max, after)
  when /(add|set)meta/
    unless @params.count == 2
      warn "Invalid filter parameters: #{@filter}(#{@params.join(",")})"
      return content
    end

    # needs to test for existing meta, setting key if exists, adding if not
    # should recognize yaml and mmd
    content.set_meta(@params[0], @params[1], style: content.meta_type)
  when /(strip|remove|delete)meta/
    if @params&.count&.positive?
      content.delete_meta(@params[0])
    else
      content.strip_meta
    end
  when /setstyle/
    # Should check for existing style first
    content.set_meta("marked style", @params[0], style: :comment)
  when /replaceall/
    unless @params.count == 2
      warn "Invalid filter parameters: #{@filter}(#{@params.join(",")})"
      return content
    end

    content.replace_all(@params[0], @params[1])
  when /replace$/
    unless @params.count == 2
      warn "Invalid filter parameters: #{@filter}(#{@params.join(",")})"
      return content
    end

    content.replace_one(@params[0], @params[1])
  when /(auto|self)link/
    content.autolink
  when /fix(head(lines|ers)|hierarchy)/
    content.fix_hierarchy
  when /(increase|decrease)headers/
    count = @params ? @params[0].to_i : 1
    @filter =~ /^inc/ ? content.increase_headers(count) : content.decrease_headers(count)
  else
    content
  end
end