Class: Propolize::PropositionalDocument

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

Overview

An object representing the propositional document which will be created by passing in source code, and then invoking the parsers to process the source code and output the HTML. The propositional document has three sections:

  1. The introduction (“intro”)

  2. The list of propositions

  3. An (optional) appendix

The document also keeps track of named and numbered footnotes. The document has three additional required properties, which are “author”, “title” and “date”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#html_escape

Constructor Details

#initialize(properties = {}) ⇒ PropositionalDocument

Returns a new instance of PropositionalDocument.



343
344
345
346
347
348
349
350
351
# File 'lib/propolize.rb', line 343

def initialize(properties = {})
  @properties = properties
  @cursor = :intro #where the document is being written to currently
  @intro = []
  @propositions = []
  @appendix = [] 
  @footnoteCount = 0
  @footnoteCountByName = {}
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



339
340
341
# File 'lib/propolize.rb', line 339

def cursor
  @cursor
end

#fileNameObject (readonly)

Returns the value of attribute fileName.



339
340
341
# File 'lib/propolize.rb', line 339

def fileName
  @fileName
end

Instance Method Details

#addHeading(heading) ⇒ Object

Add a heading (but only to the appendix - note that propositional headings are dealt with separately, because the heading of a proposition defines a new proposition)



509
510
511
512
513
514
515
516
517
518
# File 'lib/propolize.rb', line 509

def addHeading(heading)
  case @cursor
  when :intro
    raise DocumentError, "Headings are not allowed in the introduction"
  when :propositions
    raise DocumentError, "Headings are not allowed in propositions"
  when :appendix
    @appendix.push(heading)
  end
end

#addProposition(proposition) ⇒ Object

Add a new proposition (only if we are currently in either the introduction or the propositions)



471
472
473
474
475
476
477
478
479
480
# File 'lib/propolize.rb', line 471

def addProposition(proposition)
  case @cursor
  when :intro
    @cursor = :propositions
  when :appendix
    raise DocumentError, "Cannot add proposition, already in appendix"
  end
  proposition = PropositionWithExplanation.new(proposition)
  @propositions.push(proposition)
end

#addText(text) ⇒ Object

Add a textual item, depending on where we are, to the introduction, or the current proposition, or to the appendix



496
497
498
499
500
501
502
503
504
505
# File 'lib/propolize.rb', line 496

def addText(text)
  case @cursor
  when :intro
    @intro.push(text)
  when :propositions
    @propositions[-1].addExplanationItem(text)
  when :appendix
    @appendix.push(text)
  end
end

#appendixHtmlObject

Call this method in HTML template to render the appendix



432
433
434
435
436
437
438
# File 'lib/propolize.rb', line 432

def appendixHtml
  if @appendix.length == 0 then
    return ""
  else
    return "<div class=\"appendix\">\n#{@appendix.map(&:toHtml).join("\n")}\n</div>"
  end
end

#authorObject

Call this method in HTML template to write the author’s name



412
413
414
# File 'lib/propolize.rb', line 412

def author
  return @properties["author"]
end

#checkForProperty(name) ⇒ Object



364
365
366
367
368
# File 'lib/propolize.rb', line 364

def checkForProperty(name)
  if not @properties.has_key?(name)
    raise DocumentError, "No property #{name} given for document"
  end
end

#checkIsValidObject

Check that all the required properties were defined, and that at least one proposition occurred



371
372
373
374
375
376
377
378
# File 'lib/propolize.rb', line 371

def checkIsValid
  checkForProperty("title")
  checkForProperty("author")
  checkForProperty("date")
  if @cursor == :intro
    raise DocumentError, "There are no propositions in the document"
  end
end

#dateObject

Call this method in HTML template to show the date



417
418
419
# File 'lib/propolize.rb', line 417

def date
  return @properties["date"]
end

#doExtraTextReplacements(text) ⇒ Object

Special text replacements done after all other processing, currently just “–” => &ndash;



522
523
524
525
# File 'lib/propolize.rb', line 522

def doExtraTextReplacements(text)
  #puts "doExtraTextReplacements on #{text.inspect} ..."
  text.gsub!(/--/m, "&ndash;")
end

#dumpObject

Dump to stdout (for debugging/tracing)



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/propolize.rb', line 381

def dump
  puts "======================================================="
  puts "Title: #{title.inspect}"
  puts "Author: #{author.inspect}"
  puts "Date: #{date.inspect}"
  puts ""
  if @intro.length > 0 then
    puts "Introduction:"
    for item in @intro do
      puts "  #{item}"
    end
  end
  puts "Propositions:"
  for item in @propositions do
    item.dump("  ")
  end
  if @appendix.length > 0 then
    puts "Appendix:"
    for item in @appendix do
      puts "  #{item}"
    end
  end
  puts "======================================================="
end

#footnoteNumberFor(footnoteName) ⇒ Object



360
361
362
# File 'lib/propolize.rb', line 360

def footnoteNumberFor(footnoteName)
  return @footnoteCountByName[footnoteName] || "?"
end

#generateHtml(templateFileName, fileName) ⇒ Object

Generate the output HTML using an ERB template file, where the template references the methods title, author, date, propositionsHtml, appendixHtml, originalLinkHtml and fileName (applying them to ‘self’).



455
456
457
458
459
460
461
462
463
# File 'lib/propolize.rb', line 455

def generateHtml(templateFileName, fileName)
  @fileName = fileName
  #puts "  using template file #{templateFileName} ..."
  templateText = File.read(templateFileName, encoding: 'UTF-8')
  template = ERB.new(templateText)
  @binding = binding
  html = template.result(@binding)
  return html
end

#getNewFootnoteNumberFor(footnoteName) ⇒ Object



353
354
355
356
357
358
# File 'lib/propolize.rb', line 353

def getNewFootnoteNumberFor(footnoteName)
  @footnoteCount += 1
  footnoteCountString = @footnoteCount.to_s.to_s
  @footnoteCountByName[footnoteName] = footnoteCountString
  return footnoteCountString
end

#introHtmlObject

Call this method in HTML template to render the introduction



422
423
424
# File 'lib/propolize.rb', line 422

def introHtml
  return "<div class=\"intro\">\n#{@intro.map(&:toHtml).join("\n")}\n</div>"
end

#originalLinkHtmlObject

Call this method in HTML template to render the ‘original link’ (for an critique)



441
442
443
444
445
446
447
448
449
450
# File 'lib/propolize.rb', line 441

def originalLinkHtml
  if @properties.has_key? "original-link" then
    originalLinkText = @properties["original-link"]
    html = "<div class=\"original-link\">#{processText(originalLinkText)}</div>"
    puts " html = #{html.inspect}"
    return html
  else
    return ""
  end
end

#processText(text, options = {}) ⇒ Object

Process text. “text” can occur in five different contexts:

  1. Inside a link (where :weAreInsideALink gets set to true)

  2. A proposition

  3. A list item

  4. A paragraph

  5. A secondary heading (i.e. other than a proposition - currently these can only appear in the appendix)



533
534
535
536
537
538
539
540
# File 'lib/propolize.rb', line 533

def processText(text, options = {})
  weAreInsideALink = options[:weAreInsideALink]
  stringBuffer = StringBuffer.new()
  TextBeingProcessed.new(self, text, stringBuffer, weAreInsideALink).parse()
  processedText = stringBuffer.to_string()
  doExtraTextReplacements(processedText)
  return processedText
end

#propositionsHtmlObject

Call this method in HTML template to render the list of propositions



427
428
429
# File 'lib/propolize.rb', line 427

def propositionsHtml
  return "<ul class=\"propositions\">\n#{@propositions.map(&:toHtml).join("\n")}\n</ul>"
end

#setProperty(name, value) ⇒ Object

Set a property value on the document



466
467
468
# File 'lib/propolize.rb', line 466

def setProperty(name, value)
  @properties[name] = value
end

#startAppendixObject

Start the appendix (but only if we are currently in the propositions)



483
484
485
486
487
488
489
490
491
492
# File 'lib/propolize.rb', line 483

def startAppendix
  case @cursor
  when :intro
    raise DocumentError, "Cannot start appendix before any propositions occur"
  when :propositions
    @cursor = :appendix
  when :appendix
    raise DocumentError, "Cannot start appendix, already in appendix"
  end
end

#titleObject

Call this method in HTML template to write the title (and main heading)



407
408
409
# File 'lib/propolize.rb', line 407

def title
  return @properties["title"]
end