Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

208
LINES

< > BotCompany Repo | #1002213 // Eleutheria Homepage (with template & talk to bot)

JavaX source code (desktop) [tags: butter use-pretranspiled] - run with: x30.jar - homepage

Download Jar. Libraryless. Click here for Pure Java version (5001L/34K).

!7

static S defaultTemplateID = "#1002609";
static S speechTemplateID = "#1002347";
static S mobileTemplateID = "#1002450";
static S mobileSpeechTemplateID = "#1002350";

static S titleImg = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1002488&contentType=image/png";

static S news;

p {
  news =
    htag("li", "We are making an A.I. Join the community to find out more.") +
    htag("li", shortSystemName() + "'s current " + htag("a", "Turing score", "href", "http://bots.tinybrain.de/1002408") + ": " + htag("b", "$turing")) +
    htag("li", "PS: You can donate to make this project fly! -" + tag("a", "Stefan", "href", "mailto:info@superinformatiker.de"));
}

static int maxQuestionLength = 500;
static int maxAnswerLength = 200;

static S html(S subUri, Map<S, S> params) {
  S _q = params.get("q");
  boolean speech = nempty(params.get("speech"));
  boolean mobile = !empty(params.get("m"));
  boolean noredir = !empty(params.get("d"));
  
  int loaded = l((L) get(mainBot, "bots"));
  int total = l((L) get(mainBot, "botIDs"));
  
  S template;
  if (mobile)
    template = speech ? mobileSpeechTemplateID : mobileTemplateID;
  else
    template = speech ? speechTemplateID : defaultTemplateID;
  
  S html = loadSnippet(template);
  
  boolean isAuthed = webAuthed(params);

  if (noredir) {
    L<S> tok = htmlcoarsetok(html);
    int i1 = tok.indexOf([[<script id="redirect" type="text/javascript">]]);
    int i2 = indexOf(tok, "</script>", i1+1);
    print("i1=" + i1 + ", i2=" + i2);
    if (i1 >= 0 && i2 > i1) {
      clearAllTokens(tok.subList(i1, i2+1));
      html = join(tok);
    }
  }
  
  // get answer and print it!
  
  boolean def = empty(_q);
  if (def) _q = "Greet me!";
  
  if (!empty(_q)) {
    S a = trim((S) call(mainBot, "webAnswer", _q, !def));
    if (isEmpty(a))
      a = "(no response)";
    S toreplace = [[<div id="answer" class="result">]];
    try { a = encodeAnswerForWeb(a); }
    catch (Throwable e) { printStackTrace(e); a = htmlencode(a); }
    html = html.replace(toreplace, toreplace + "<b>" + system() + ":</b> " + a);
  }
  
  // title image
  
  S origImg = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&amp;file=blob_1002279&amp;contentType=image/png";
  //html = html.replace(origImg, "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1002409&contentType=image/png");
  html = html.replace(origImg, titleImg);
  
  // link
  
  html = html.replace(htmlQuote("http://tinybrain.de"), htmlQuote("http://sister.tinybrain.de"));
  
  // fix note
  
  html = html.replace(".It learns about computers first, then humans.", ". It learns about computers first, then humans. " + hrefBlank("http://bots.tinybrain.de", "Bot list."));
  
  
  // system name
  
  html = html.replace("Eleutheria", system());
  html = html.replace("International International", "International"); // one is enough!
  
  S systemURL = systemURL();
  if (nempty(systemURL)) pcall {
    int idx = html.indexOf("</title>");
    html = html.substring(0, idx+1) +
      html.substring(idx+1).replace(system() + " is learning how to think",
        tag("a", system(), "href", systemURL) + " is learning how to think");
  }
  
  // show if authed
  
  if (isAuthed)
    html = html.replace("Talk to the robot", "Talk to the robot (you are authorized)");
   
  // no talk for not authed 
  if (!isAuthed)
    html = deleteFromTo(html, "<form name=\"talk-form\"", "</form>");
  
  //html = html.replace("PUT # OF BOTS HERE", loaded + "/" + total);
  
  L history = cast get(mainBot, "history");
  new StringBuffer buf;
  pcall {
    int x = max(l(history)-5, 0);
    print("l(history)=" + l(history) + ", x=" + x);
    for (int i = l(history)-1; i >= x; i--) {
      O dialog = history.get(i);
      S q = shorten(getString(dialog, "question"), maxQuestionLength);
      S a = shorten(getString(dialog, "answer"), maxAnswerLength);
      buf.append(htag("li", "User: <b>" + htmlencode(q) + "</b>", "class", "splash-li") + "\n");
      buf.append(htag("li", system() + ": <b>" + htmlencode(a) + "</b>", "class", "splash-li") + "\n");
    }
  }
  
  L<S> tok = htmlcoarsetok(html);
  
  // Last dialogs
  
  int i1 = tok.indexOf("<ul class=\"splash-ul\">");
  int i2 = indexOf(tok, "</ul>", i1+1);
  //print("i1=" + i1 + ", i2= " + i2);
  if (i1 >= 0 && i2 > i1) {
    clearAllTokens(tok.subList(i1+1, i2));
    tok.set(i1+1, str(buf));
  }
  
  // News
  
  i1 = indexOf(tok, "<ul class=\"splash-ul\">", i1+1);
  i2 = indexOf(tok, "</ul>", i1+1);
  if (i1 >= 0 && i2 > i1) {
    clearAllTokens(tok.subList(i1+1, i2));
    S dehnews = news;
    pcall {
      S score = or((S) callOpt(getBot("#1002408"), "getTuringScore"), "not calculated yet");
      dehnews = dehnews.replace("$turing", score);
    }
    tok.set(i1+1, dehnews);
  }
  
  O visitorsBot = callOpt(mainBot, "getBot", "#1002157");
  S visitorStats = visitorsBot == null ? null : str(callHtmlMethod(visitorsBot, "/"));
  
  if (nempty(visitorStats)) pcall {
    //html = "<!--\n" + visitorStats + "\n-->\n" + html;
    
    // Coolbox
    
    visitorStats = visitorStats.substring(visitorStats.indexOf("<br>") + 4);
  
    int i = tok.indexOf("</center>");
    if (i < 0) i = tok.indexOf("<div class=\"boxcontainer\">");
    if (i >= 0)
      tok.set(i, tok.get(i) +
    [[
      <div class="coolbox">
        <div class="boxtitle"><h1>$title</h1></div>
        <div class="boxcontent">$content</div>
      </div>
    ]].replace("$title", "Visitor Stats")
      .replace("$content", htag("p", visitorStats)));
  }
  
  html = join(tok);
  
  html = html.replace("(latest first)", "(latest first, " + l(history) + " total)");
  
  // Bot List link
  
  S botsList = "/1002771";
  html = html.replace("=\"http://bots.tinybrain.de/\"" , "=\"" + botsList + "\"");
  html = html.replace("=\"http://bots.tinybrain.de\"", "=\"" + botsList + "\"");
  
  ret html;
}

static S encodeAnswerForWeb(S s) {
  s = s.trim();
  
  new Matches m;
  S s2 = dropPrefixIgnoreCase("lookie here: ", s);
  if (neq(s, s2) || s.contains(".gstatic.com/images?") || s.startsWith("http://tinyurl.com/")) {
    S url = s2.trim();
    print("url: " + quote(url));
    if (isURL(url))
      ret "<br>" + tag("img", "", "src", url);
  }
  
  if (isURL(s))
    ret tag("a", htmlencode(s), "href", s, "target", "_blank");

  new StringBuilder buf;
  int i = 0;
  while (i < l(s)) {
    int i1 = smartIndexOf(s, "```", i);
    int i2 = smartIndexOf(s, "```", i1+3);
    buf.append(htmlencode(s.substring(i, i1)));
    if (i2 > i1)
      buf.append(sourceCodeToHTML(s.substring(i1+3, i2)));
    i = i2+3;
  }
  ret str(buf);
}

Author comment

Began life as a copy of #1002150

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002213
Snippet name: Eleutheria Homepage (with template & talk to bot)
Eternal ID of this version: #1002213/3
Text MD5: 3552542438362ed578a4bd8c16e1bfc2
Transpilation MD5: 41728150f17fdcc28f6085f713961396
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-12-15 20:08:22
Source code size: 6851 bytes / 208 lines
Pitched / IR pitched: No / No
Views / Downloads: 1069 / 9158
Version history: 2 change(s)
Referenced in: [show references]