package com.github.ferstl.depgraph.dot;

import java.util.Map;
import java.util.TreeMap;
import com.google.common.html.HtmlEscapers;

/**
 * <a href=http://www.graphviz.org/doc/info/shapes.html#html>HTML-like labels</a>.
 */
public class LabelBuilder {

  private final StringBuilder labelBuilder = new StringBuilder();

  public LabelBuilder text(String text) {
    this.labelBuilder.append(HtmlEscapers.htmlEscaper().escape(text));
    return this;
  }

  public LabelBuilder italic(String text) {
    addText(text, "i");
    return this;
  }

  public LabelBuilder bold(String text) {
    addText(text, "b");
    return this;
  }

  public LabelBuilder underline(String text) {
    addText(text, "u");
    return this;
  }

  public LabelBuilder newLine() {
    this.labelBuilder.append("<br/>");
    return this;
  }

  public FontBuilder font() {
    return new FontBuilder();
  }

  public String build() {
    return "<" + this.labelBuilder + ">";
  }

  private void addText(String text, String tagName, String... tagAttributes) {
    // tag with attributes
    this.labelBuilder.append("<").append(tagName);
    for (String attribute : tagAttributes) {
      this.labelBuilder.append(" ").append(attribute);
    }
    this.labelBuilder.append(">");

    // text and end tag
    this.labelBuilder
        .append(HtmlEscapers.htmlEscaper().escape(text))
        .append("</").append(tagName).append(">");
  }

  public class FontBuilder {

    private final Map<String, String> attributes = new TreeMap<>();

    public FontBuilder color(String color) {
      addAttribute("color", color);
      return this;
    }

    public FontBuilder size(int size) {
      if (size < 0) {
        throw new IllegalArgumentException("Font size must not be negative");
      }
      addAttribute("point-size", size);
      return this;
    }

    public FontBuilder name(String font) {
      addAttribute("face", font);
      return this;
    }

    public LabelBuilder text(String text) {
      LabelBuilder.this.addText(text, "font", this.attributes.values().toArray(new String[0]));
      return LabelBuilder.this;
    }

    private void addAttribute(String name, Object value) {
      this.attributes.put(name, name + "=\"" + value + "\"");
    }
  }
}

@@ -364,73 +364,21 @@
     
-this.labelBuilder.append(HtmlEscapers.htmlEscaper().escape
+addText
 (text)
-)
 ;%0A  
@@ -1215,12 +1215,35 @@
 text
- and
+%0A    addText(text);%0A%0A    //
  end
@@ -1272,114 +1272,162 @@
 lder
-%0A        .append(HtmlEscapers.htmlEscaper().escape(text))%0A        .append(%22%3C/%22).append(tagName).append(%22%3E%22
+.append(%22%3C/%22).append(tagName).append(%22%3E%22);%0A  %7D%0A%0A  private void addText(String text) %7B%0A    this.labelBuilder.append(HtmlEscapers.htmlEscaper().escape(text)
 );%0A 

Add addText() method without tags