class ReferenceTweaker

This class provides methods to tweak the reference according to the target document format

Attributes

log[RW]

This attribute keeps the target format

target[RW]

This attribute keeps the target format

Public Class Methods

new(target, logger=nil) click to toggle source
constructor
:target: the target format
         in which the referneces shall be represented

todo: improve logger approach

# File lib/wortsammler/class.proolib.rb, line 191
def initialize(target, logger=nil)
  @target=target

  @log=logger || $logger || nil

  if @log == nil
    @log                 = Logger.new(STDOUT)
    @log.level           = Logger::INFO
    @log.datetime_format = "%Y-%m-%d %H:%M:%S"
    @log.formatter       = proc do |severity, datetime, progname, msg|
      "#{datetime}: #{msg}\n"
    end
  end
end

Public Instance Methods

prepareFile(infile, outfile) click to toggle source

this does the postprocessing of the file in particluar handle wortsammler's specific syntax.

# File lib/wortsammler/class.proolib.rb, line 209
def prepareFile(infile, outfile)

  infileIo=File.new(infile)
  text    = infileIo.readlines.join
  infileIo.close

  #include pdf files

  if @target == "pdf"
    text.gsub!(INCLUDE_PDF_PATTERN) { |m|

      if $4
        pages="[pages=#{$4}]"
      else
        pages=""
      end

      if $5
        clearpage=$5
      else
        clearpage="cleardoublepage"
      end

      if $3.length > 0
        level=$3
      else
        level=9
      end

      "\n\n\\#{clearpage}\n\\bookmark[level=#{level},page=\\thepage]{#{$2}}\n\\includepdf#{pages}{#{$1}}"
    }
  else #if not pdf then it gets a regular external link
    text.gsub!(INCLUDE_PDF_PATTERN) { |m|
      "[#{$2}](#{$1})"
    }
  end

  # include Markdown files
  #
  #
  text = replace_md_inlay(text)


  # embed images
  #
  if @target == "pdf"
    text.gsub!(EMBEDDED_IMAGE_PATTERN) { |m|
      "\\wsembedimage{#{$1}}{#{$2}}{#{$3}}{#{$4}}"
    }
  else #if not pdf then it gets a regular image
    text.gsub!(EMBEDDED_IMAGE_PATTERN) { |m|
      "![#{$1}](#{$1})"
    }
  end

  #inject the anchors for references to traces ->[traceid]
  if @target == "pdf" then
    text.gsub!(TRACE_ANCHOR_PATTERN) { |m| "[#{$1}]#{$2}\\hypertarget{#{mkInternalTraceId($1)}}{}" }
  else
    text.gsub!(TRACE_ANCHOR_PATTERN) { |m| "<a id=\"#{mkInternalTraceId($1)}\">[#{$1}]</a>#{$2}" }
  end

  #substitute arbitrary anchors for arbitrary targets <a id="">
  if @target == "pdf" then
    text.gsub!(ANY_ANCHOR_PATTERN) { |m| "\\hypertarget{#{mkInternalTraceId($1)}}{}" }
  else
    # it is already html
  end

  #substitute arbitrary document internal references <a href=""></a>
  if @target == "pdf" then
    text.gsub!(ANY_REF_PATTERN) { |m| "\\hyperlink{#{$1}}{#{mkTexTraceDisplay($2)}}" }
  else
    # it is already html
  end

  # substitute the uptrace references
  text.gsub!(UPTRACE_REF_PATTERN) { |m| "}(#{prepareTraceReferences($1)})" }

  # substitute the informal trace references
  text.gsub!(TRACE_REF_PATTERN) { |m| "[#{prepareTraceReferences($1)}]" }


  # substitute expected Results
  #
  #
  if @target == "pdf" then
    text.gsub!(EXPECTED_RESULT_PATTERN) { |m| "#{prepareExpectedResults($1, $2, $3)}" }
  else
    # it is already leave it as it is
  end

  # substitute plantuml
  #
  # note this is substituted in any case
  #
  #if @target == "pdf" then
  text.gsub!(PLANTUML_PATTERN) { |m| "" }

  #else
  # it is already leave it as it is
  #end

  File.open(outfile, "w") { |f| f.puts(text) }
end

Private Instance Methods

mkInternalTraceId(string) click to toggle source

this tweaks the reference-Id to be comaptible as TeX label private methd

# File lib/wortsammler/class.proolib.rb, line 143
def mkInternalTraceId(string)
  string.gsub("_", "-")
end
mkTexTraceDisplay(trace) click to toggle source

this tweaks the reference-id to be displayed in TeX private method

# File lib/wortsammler/class.proolib.rb, line 149
def mkTexTraceDisplay(trace)
  trace.gsub("_", "\\_")
end
prepareExpectedResults(indent="", original_label, body) click to toggle source
prepareExpectedResults description

@param label [type] [description] @param body [type] [description]

@return [type] [description]

# File lib/wortsammler/class.proolib.rb, line 114
def prepareExpectedResults(indent="", original_label, body)
  result_items=body.split("-   ")[1..-1].map { |i| i.strip }
  result      = ["\\begin{Form}"]

  label=original_label.gsub(/_/, "-")
  j    ="00"
  result << result_items.map { |i|
    j = j.next
    "\\CheckBox[name=#{label}-#{j}]{} #{i}"
  }
  result << "\\vspace{1em}"
  result << "\\ChoiceMenu[combo, name=#{label}-verdict, default=none]{Test verdict:}{none, ok-30, ok-60, ok, fail, pending}"
  result << "\\vspace{1em}"
  result << ["\\TextField[ name=#{label}-comment , width=40em, height=2cm, multiline=true, backgroundcolor={0.9 0.9 0.9}] {}"]
  result << ["\\end{Form}"]

  unless $1.nil? then
    leading_whitespace=$1.split("\n", 100)
    leading_lines     =leading_whitespace[0..-1].join("\n")
    leading_spaces    =leading_whitespace.last || ""
    replacetext       =leading_lines+replacetext_raw.gsub("\n", "\n#{leading_spaces}")
  end

  result=result.compact.flatten.map { |i| "#{indent}#{i}" }
  result.join("\n#{indent}\n")
end
prepareTraceReferences(string) click to toggle source

this prepares the reference in the target format

:string: the Id of the referenced Traceable

# File lib/wortsammler/class.proolib.rb, line 94
def prepareTraceReferences(string)
  result=string.gsub(/\s*/, "").split(",").map { |trace|
    itrace   = mkInternalTraceId(trace)
    texTrace = mkTexTraceDisplay(trace)
    if @target == "pdf" then
      "\\hyperlink{#{itrace}}{#{texTrace}}"
    else
      "[#{trace}](\##{itrace})"
    end
  }
  result.join(", ")
end
replace_md_inlay(text) click to toggle source

This replaces markdown inlays it is a subroutine which is called recursively todo: handle indentation

@param text [String] text in which the markdown inlays shall be processed @return [String] The resulting text

# File lib/wortsammler/class.proolib.rb, line 162
def replace_md_inlay(text)
  text.gsub!(INCLUDE_MD_PATTERN) { |m|
    infile = $2.gsub("\\_", "_")
    if File.exist?(infile) then
      replacetext_raw = File.open(infile, :encoding => 'bom|utf-8').read
      unless $1.nil? then
        leading_whitespace = $1.split("\n", 100)
        leading_lines      = leading_whitespace[0 .. -1].join("\n")
        leading_spaces     = leading_whitespace.last || ""
        replacetext        = leading_lines + replacetext_raw.gsub("\n", "\n#{leading_spaces}")
      else
        replacetext = replacetext_raw
      end
    else
      replacetext = ""
      @log.warn("File not found: #{$2}")
    end
    result = replace_md_inlay(replacetext)
    result
  }
  text
end