Class: Propolize::PropositionalDocument
- Inherits:
-
Object
- Object
- Propolize::PropositionalDocument
- 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:
-
The introduction (“intro”)
-
The list of propositions
-
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
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#fileName ⇒ Object
readonly
Returns the value of attribute fileName.
Instance Method Summary collapse
-
#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).
-
#addProposition(proposition) ⇒ Object
Add a new proposition (only if we are currently in either the introduction or the propositions).
-
#addText(text) ⇒ Object
Add a textual item, depending on where we are, to the introduction, or the current proposition, or to the appendix.
-
#appendixHtml ⇒ Object
Call this method in HTML template to render the appendix.
-
#author ⇒ Object
Call this method in HTML template to write the author’s name.
- #checkForProperty(name) ⇒ Object
-
#checkIsValid ⇒ Object
Check that all the required properties were defined, and that at least one proposition occurred.
-
#date ⇒ Object
Call this method in HTML template to show the date.
-
#doExtraTextReplacements(text) ⇒ Object
Special text replacements done after all other processing, currently just “–” => –.
-
#dump ⇒ Object
Dump to stdout (for debugging/tracing).
- #footnoteNumberFor(footnoteName) ⇒ Object
-
#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’).
- #getNewFootnoteNumberFor(footnoteName) ⇒ Object
-
#initialize(properties = {}) ⇒ PropositionalDocument
constructor
A new instance of PropositionalDocument.
-
#introHtml ⇒ Object
Call this method in HTML template to render the introduction.
-
#originalLinkHtml ⇒ Object
Call this method in HTML template to render the ‘original link’ (for an critique).
-
#processText(text, options = {}) ⇒ Object
Process text.
-
#propositionsHtml ⇒ Object
Call this method in HTML template to render the list of propositions.
-
#setProperty(name, value) ⇒ Object
Set a property value on the document.
-
#startAppendix ⇒ Object
Start the appendix (but only if we are currently in the propositions).
-
#title ⇒ Object
Call this method in HTML template to write the title (and main heading).
Methods included from Helpers
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
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
339 340 341 |
# File 'lib/propolize.rb', line 339 def cursor @cursor end |
#fileName ⇒ Object (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 |
#appendixHtml ⇒ Object
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 |
#author ⇒ Object
Call this method in HTML template to write the author’s name
412 413 414 |
# File 'lib/propolize.rb', line 412 def 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 |
#checkIsValid ⇒ Object
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 |
#date ⇒ Object
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 “–” => –
522 523 524 525 |
# File 'lib/propolize.rb', line 522 def doExtraTextReplacements(text) #puts "doExtraTextReplacements on #{text.inspect} ..." text.gsub!(/--/m, "–") end |
#dump ⇒ Object
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: #{.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 |
#introHtml ⇒ Object
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 |
#originalLinkHtml ⇒ Object
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:
-
Inside a link (where :weAreInsideALink gets set to true)
-
A proposition
-
A list item
-
A paragraph
-
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, = {}) weAreInsideALink = [:weAreInsideALink] stringBuffer = StringBuffer.new() TextBeingProcessed.new(self, text, stringBuffer, weAreInsideALink).parse() processedText = stringBuffer.to_string() doExtraTextReplacements(processedText) return processedText end |
#propositionsHtml ⇒ Object
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 |
#startAppendix ⇒ Object
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 |
#title ⇒ Object
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 |