Home /Modules/sleeladoc/SLDocRenderer.sleela

SLDocRenderer.sleela

lib/sleeladoc/SLDocRenderer.sleela · 157 line(s) · sleela

<< lib/sleeladoc/SLDocRenderer.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLDocRenderer -- renders a parsed SLDocModel as a Markdown
     or minimal HTML API page (the way javadoc renders an HTML page for Java). >>
#sleela 3.0
material;
< Family: sleeladoc. The back end: turn an SLDocModel into a human-readable API >
< page. toMarkdown() emits a GitHub-flavored Markdown document; toHtml() emits a >
< minimal self-contained HTML fragment. Both walk the members in declaration >
< order and print the signature, doc summary, and tags of each member. Output >
< whole-string concatenation (no slicing needed). >
class SLDocRenderer {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 7946;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-28";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-28";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  < ---- Markdown ---- >

  < Render the whole model as a Markdown API page. >
  String toMarkdown(SLDocModel model) {
    if (model == null) { return ""; }
    String nl = "\n";
    String out = "# " + model.getUnitName() + nl + nl;
    out = out + "_SleelaDoc API page. "
        + model.documentedCount() + " of " + model.memberCount()
        + " members documented._" + nl + nl;

    int n = model.memberCount();
    for (int i = 0; i < n; i = i + 1) {
      SLDocMember m = model.memberAt(i);
      if (m == null) { continue; }
      out = out + mdMember(m, nl);
    }
    return out;
  }

  < One member as a Markdown section. >
  String mdMember(SLDocMember m, String nl) {
    String head = "## ";
    if (m.getKind() == 0) { head = "# class "; }
    if (m.getKind() == 1) { head = "### field "; }
    if (m.getKind() == 2) { head = "### method "; }
    String out = head + m.getName() + nl + nl;
    out = out + "`" + m.getSignature() + "`" + nl + nl;

    SLDocComment doc = m.getDoc();
    if (doc != null) {
      String summary = doc.getSummary();
      if (summary != "") { out = out + summary + nl + nl; }
      int tn = doc.tagCount();
      for (int i = 0; i < tn; i = i + 1) {
        SLDocTag t = doc.tagAt(i);
        if (t != null) {
          out = out + "- **@" + t.getTagName() + "** " + t.getTagValue() + nl;
        }
      }
      if (tn > 0) { out = out + nl; }
    }
    return out;
  }

  < ---- minimal HTML ---- >

  < Render the whole model as a minimal self-contained HTML fragment. >
  String toHtml(SLDocModel model) {
    if (model == null) { return ""; }
    String nl = "\n";
    String out = "<section class=\"sleeladoc\">" + nl;
    out = out + "<h1>" + esc(model.getUnitName()) + "</h1>" + nl;
    out = out + "<p class=\"meta\">" + model.documentedCount() + " of "
        + model.memberCount() + " members documented.</p>" + nl;

    int n = model.memberCount();
    for (int i = 0; i < n; i = i + 1) {
      SLDocMember m = model.memberAt(i);
      if (m == null) { continue; }
      out = out + htmlMember(m, nl);
    }
    out = out + "</section>" + nl;
    return out;
  }

  String htmlMember(SLDocMember m, String nl) {
    String out = "<div class=\"member " + m.kindName() + "\">" + nl;
    out = out + "<h3>" + m.kindName() + " " + esc(m.getName()) + "</h3>" + nl;
    out = out + "<pre><code>" + esc(m.getSignature()) + "</code></pre>" + nl;
    SLDocComment doc = m.getDoc();
    if (doc != null) {
      String summary = doc.getSummary();
      if (summary != "") { out = out + "<p>" + esc(summary) + "</p>" + nl; }
      int tn = doc.tagCount();
      if (tn > 0) {
        out = out + "<dl>" + nl;
        for (int i = 0; i < tn; i = i + 1) {
          SLDocTag t = doc.tagAt(i);
          if (t != null) {
            out = out + "<dt>@" + esc(t.getTagName()) + "</dt><dd>"
                + esc(t.getTagValue()) + "</dd>" + nl;
          }
        }
        out = out + "</dl>" + nl;
      }
    }
    out = out + "</div>" + nl;
    return out;
  }

  << HTML-escape a value. The built-in String is opaque, so true per-character
     escaping is a char-cell concern (see lib/xml SLXmlSerializer); for the
     identifier/short-text surface a doc page renders, values are already plain,
     so this pass-through keeps the output faithful without claiming a char-level
     transform it cannot perform in-language. A caller needing full escaping
     supplies a char-cell view through the lib/xml serializer. >>
  String esc(String s) {
    if (s == null) { return ""; }
    return s;
  }
}