ruby/blog/update_post.rb
# Modifies line in place during the first pass through a file, and writes
# extra output to buffer if necessary.
#
# line should not be written to buffer directly.
Modifies line in place during the first pass through a file, and writes
extra output to buffer if necessary.

line should not be written to buffer directly.
Modifies line in place during the first pass through a file and writes extra output to buffer if necessary . line should not be written to buffer directly .
def parse_line_first_pass(line, buffer)
    if line == "<div class=\"footnote\">\n"
      @in_footnote_div = true
      @i = 1
    end
    if not @in_footnote_div
      start = 0
      while match = line.match(NEW_NOTE, start)
        title, note_text = match[1], match[2]
        replacement = "[\"(##{title}-r0). ^0^\":##{title}-0]"
        line.replace "#{match.pre_match}#{replacement}#{match.post_match}"
        start = match.pre_match.length + replacement.length
        @new_notes.push note_text
        @num_new_notes += 1
      end
      start = 0
      while match = line.match(REF, start)
        title, index = match[1], match[2]
        replacement = "[\"(##{title}-r#{@i}). ^#{@i}^\":##{title}-#{@i}]"
        line.replace "#{match.pre_match}#{replacement}#{match.post_match}"
        start = match.pre_match.length + replacement.length
        @notes.push((index != '0') ? '' :
                    "[\"(##{title}-#{@i})\. ^#{@i}^\":##{title}-r#{@i}]" +
                    "#{@new_notes.shift}")
        @i += 1
      end
    elsif match = line.match(TARGET)
      ref, title = match[0], match[1]
      while @i < @notes.size and not @notes[@i].empty?
        buffer.write "#{@notes[@i]}\n\n"
        @i += 1
      end
      if @i == @notes.size
        $stderr.puts("Only #{@i - 1} references or new notes parsed, " +
                     "but expected more; look for malformed new notes, " +
                     "or malformed or missing references for existing " +
                     "footnotes\n")
        exit 1
      end
      line.sub!(ref, "[\"(##{title}-#{@i}). ^#{@i}^\":##{title}-r#{@i}]")
      @i += 1
    elsif line == "</div>\n" and @i < @notes.size
      @notes[@i..@notes.size].each {|note| buffer.write "\n#{note}\n"}
    end
  end
def parse_line_first_pass ( line , buffer ) if line == "<div class=\"footnote\">\n" @in_footnote_div = true @i = 1 end if not @in_footnote_div start = 0 while match = line . match ( NEW_NOTE , start ) title , note_text = match [ 1 ] , match [ 2 ] replacement = "[\"(##{title}-r0). ^0^\":##{title}-0]" line . replace "#{match.pre_match}#{replacement}#{match.post_match}" start = match . pre_match . length + replacement . length @new_notes . push note_text @num_new_notes += 1 end start = 0 while match = line . match ( REF , start ) title , index = match [ 1 ] , match [ 2 ] replacement = "[\"(##{title}-r#{@i}). ^#{@i}^\":##{title}-#{@i}]" line . replace "#{match.pre_match}#{replacement}#{match.post_match}" start = match . pre_match . length + replacement . length @notes . push ( ( index != '0' ) ? '' : "[\"(##{title}-#{@i})\. ^#{@i}^\":##{title}-r#{@i}]" + "#{@new_notes.shift}" ) @i += 1 end elsif match = line . match ( TARGET ) ref , title = match [ 0 ] , match [ 1 ] while @i < @notes . size and not @notes [ @i ] . empty? buffer . write "#{@notes[@i]}\n\n" @i += 1 end if @i == @notes . size $stderr . puts ( "Only #{@i - 1} references or new notes parsed, " + "but expected more; look for malformed new notes, " + "or malformed or missing references for existing " + "footnotes\n" ) exit 1 end line . sub! ( ref , "[\"(##{title}-#{@i}). ^#{@i}^\":##{title}-r#{@i}]" ) @i += 1 elsif line == "</div>\n" and @i < @notes . size @notes [ @i .. @notes . size ] . each { | note | buffer . write "\n#{note}\n" } end end
Modifies line in place during the first pass through a file, and writes
extra output to buffer if necessary.
Modifies line in place during the first pass through a file and writes extra output to buffer if necessary .
# First pass: Expand any new footnote references and capture their # text. # Use zeros as index placeholders until the next pass. # Second pass: Update footnote numbers. # Insert new notes before this preexisting one. # Error recovery: We shouldn't exhaust @notes unless a new note or # existing reference failed to parse. # Insert remaining notes just before closing the div.
line
<not_specific>
buffer
<not_specific>