/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Sections preset. 
  (two token classes in one file)
  Breaks given text into hierarchy of 
  "wakaSections" tokens.
  
  == Section level 1
  === Section level 2
  ...
  ====== Section level 5

*************************************/

function wakaSections() { this.isSectionHeader = true; }
         wakaSections.prototype = new wakaToken();
         wakaSections.prototype.constructor = 
         wakaSections;

// static
wakaSections.prototype.getRegexpPart = function()
{
  return "((^|\n)[\r\t ]*={2,6}.*)";
}
                               //   1          2       3
wakaSections.prototype.isRE  = /^\s*(={2,6})\s*(.*?)\s*(==+)?\s*?$/;
wakaSections.prototype.is = function( outerText )
{
  var matches = this.isRE.exec( outerText );
  if (matches !== null) 
    return {
            inner : matches[2],
            level : matches[1].length-1,
            text  : outerText
           };
  return false;
}

// private (up to parent)
wakaSections.prototype._glue = function()
{
  // glue only if prev is wakaSections with lesser deep
  if (this._prev && (this._prev.isSectionHeader)
                 && (this._prev._data.level < this._data.level))
    return this._prev.glueChild( this );
  else
    return false;
}

// this is a hack to trim newlines after section markup
wakaSections.prototype.hack_separateTrimNextNewline = true;


// returns separation from a given "next"
wakaSections.prototype.separateFromNext   = function( next )
{
  return "</div></div>";
}

// private (compilation)
wakaSections.prototype._compile   = function()
{
  var inner = this.wf.format( this._data.inner, "default" );
  var glued = this.wf.formatTree( this._tree, "default" );

  var _css1 = this.wf.cssPrefix + "section" + this.wf.cssPostfix;
  var _css2 = this.wf.cssPrefix + "section-content" + this.wf.cssPostfix;

  this._html = '<div class="'+_css1+'">'+
               "<h"+this._data.level+">"+ inner +"</h"+this._data.level+">"+
               '<div class="'+_css2+'">'+
               glued;
}


// make a break, if exporting to text
wakaSections.prototype.toText = function()
{
  var sign = "=";
  for (var i=0; i< this._data.level; i++) 
    sign+="=";
  return "\n"+sign + " " + this._data.inner + "\n";
}

/************************************

  here starts second token of a preset,
  which denotes CONTENT of a section,
  not its title.

*************************************/


function wakaSectionsContent() {}
         wakaSectionsContent.prototype = new wakaToken();
         wakaSectionsContent.prototype.constructor = 
         wakaSectionsContent;

// private (up to parent)
wakaSectionsContent.prototype._glue = function()
{
  if (this._data == "") return true;

  // glue only if prev is wakaSections
  if (this._prev && (this._prev.isSectionHeader))
    return this._prev.glueChild( this );
  else
    return false;
}

