Class: Propolize::DocumentChunks

Inherits:
Object
  • Object
show all
Defined in:
lib/propolize.rb

Overview

Propolize source code is parsed as a series of “document chunks”. (Each chunk is then converted to a “document component” by calling getDocumentComponent

and each component is then "written" to the output document by calling writeToDocument.)

Instance Method Summary collapse

Constructor Details

#initialize(srcText) ⇒ DocumentChunks

Returns a new instance of DocumentChunks.



886
887
888
# File 'lib/propolize.rb', line 886

def initialize(srcText)
  @srcText = srcText
end

Instance Method Details

#createInitialChunkFromLine(line) ⇒ Object

Knowing that we are starting a new “chunk”, determine type of chunk based on the first line



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/propolize.rb', line 891

def createInitialChunkFromLine(line)
  specialLineMatch = line.match(/^\#\#([a-z\-]*)\s*(.*)$/) # does it start with "##<identifier>" with optional addition value?, and identifier is alphabetic or "-"?
  if specialLineMatch then
    return SpecialChunk.new(specialLineMatch[1], specialLineMatch[2])
  else
    specialTagMatch = line.match(/^\#:([a-z\-0-9]+)\s*(.*)$/) # does it start with "#:<alphanumeric-identifier>"?
    if specialTagMatch then
      return ParagraphChunk.new(specialTagMatch[2], :tag => specialTagMatch[1]) # paragraph with special tag
    else
      propositionMatch = line.match(/^\#\s*(.*)$/) # does it start with "# "?
      if propositionMatch then
        return PropositionChunk.new(propositionMatch[1]) # proposition
      else
        critiqueMatch = line.match(/^\?\?\s(.*)$/) # does it start with "?? " ?
        isCritique = critiqueMatch != nil # if so, this is a "critique" item
        if isCritique then
          line = critiqueMatch[1] # strip off the "?? " prefix to process the rest of the line
        end
        listMatch = line.match(/^\*\s+/) # does it start with "* " ?
        if listMatch then
          return ListChunk.new(line, :isCritique => isCritique) # list of items
        else
          blankLineMatch = line.match(/^\s*$/) # is it a blank line
          if blankLineMatch then
            return nil # blank line, so actually we don't start a new chunk yet
          else
            return ParagraphChunk.new(line, :isCritique => isCritique) # anything else, must be a paragraph
          end
        end
      end
    end
  end
end

#eachObject



925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'lib/propolize.rb', line 925

def each
  currentChunk = nil
  for line in @srcText.split("\n") do
    if currentChunk == nil then
      currentChunk = createInitialChunkFromLine(line)
    else
      if currentChunk.isTerminatedBy?(line)
        yield currentChunk.postProcess
        currentChunk = createInitialChunkFromLine(line)
      else
        currentChunk.addLine(line)
      end
    end
  end
  if currentChunk
    yield currentChunk.postProcess
  end
end