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).

1  
!7
2  
3  
static S defaultTemplateID = "#1002609";
4  
static S speechTemplateID = "#1002347";
5  
static S mobileTemplateID = "#1002450";
6  
static S mobileSpeechTemplateID = "#1002350";
7  
8  
static S titleImg = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1002488&contentType=image/png";
9  
10  
static S news;
11  
12  
p {
13  
  news =
14  
    htag("li", "We are making an A.I. Join the community to find out more.") +
15  
    htag("li", shortSystemName() + "'s current " + htag("a", "Turing score", "href", "http://bots.tinybrain.de/1002408") + ": " + htag("b", "$turing")) +
16  
    htag("li", "PS: You can donate to make this project fly! -" + tag("a", "Stefan", "href", "mailto:info@superinformatiker.de"));
17  
}
18  
19  
static int maxQuestionLength = 500;
20  
static int maxAnswerLength = 200;
21  
22  
static S html(S subUri, Map<S, S> params) {
23  
  S _q = params.get("q");
24  
  boolean speech = nempty(params.get("speech"));
25  
  boolean mobile = !empty(params.get("m"));
26  
  boolean noredir = !empty(params.get("d"));
27  
  
28  
  int loaded = l((L) get(mainBot, "bots"));
29  
  int total = l((L) get(mainBot, "botIDs"));
30  
  
31  
  S template;
32  
  if (mobile)
33  
    template = speech ? mobileSpeechTemplateID : mobileTemplateID;
34  
  else
35  
    template = speech ? speechTemplateID : defaultTemplateID;
36  
  
37  
  S html = loadSnippet(template);
38  
  
39  
  boolean isAuthed = webAuthed(params);
40  
41  
  if (noredir) {
42  
    L<S> tok = htmlcoarsetok(html);
43  
    int i1 = tok.indexOf([[<script id="redirect" type="text/javascript">]]);
44  
    int i2 = indexOf(tok, "</script>", i1+1);
45  
    print("i1=" + i1 + ", i2=" + i2);
46  
    if (i1 >= 0 && i2 > i1) {
47  
      clearAllTokens(tok.subList(i1, i2+1));
48  
      html = join(tok);
49  
    }
50  
  }
51  
  
52  
  // get answer and print it!
53  
  
54  
  boolean def = empty(_q);
55  
  if (def) _q = "Greet me!";
56  
  
57  
  if (!empty(_q)) {
58  
    S a = trim((S) call(mainBot, "webAnswer", _q, !def));
59  
    if (isEmpty(a))
60  
      a = "(no response)";
61  
    S toreplace = [[<div id="answer" class="result">]];
62  
    try { a = encodeAnswerForWeb(a); }
63  
    catch (Throwable e) { printStackTrace(e); a = htmlencode(a); }
64  
    html = html.replace(toreplace, toreplace + "<b>" + system() + ":</b> " + a);
65  
  }
66  
  
67  
  // title image
68  
  
69  
  S origImg = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&amp;file=blob_1002279&amp;contentType=image/png";
70  
  //html = html.replace(origImg, "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1002409&contentType=image/png");
71  
  html = html.replace(origImg, titleImg);
72  
  
73  
  // link
74  
  
75  
  html = html.replace(htmlQuote("http://tinybrain.de"), htmlQuote("http://sister.tinybrain.de"));
76  
  
77  
  // fix note
78  
  
79  
  html = html.replace(".It learns about computers first, then humans.", ". It learns about computers first, then humans. " + hrefBlank("http://bots.tinybrain.de", "Bot list."));
80  
  
81  
  
82  
  // system name
83  
  
84  
  html = html.replace("Eleutheria", system());
85  
  html = html.replace("International International", "International"); // one is enough!
86  
  
87  
  S systemURL = systemURL();
88  
  if (nempty(systemURL)) pcall {
89  
    int idx = html.indexOf("</title>");
90  
    html = html.substring(0, idx+1) +
91  
      html.substring(idx+1).replace(system() + " is learning how to think",
92  
        tag("a", system(), "href", systemURL) + " is learning how to think");
93  
  }
94  
  
95  
  // show if authed
96  
  
97  
  if (isAuthed)
98  
    html = html.replace("Talk to the robot", "Talk to the robot (you are authorized)");
99  
   
100  
  // no talk for not authed 
101  
  if (!isAuthed)
102  
    html = deleteFromTo(html, "<form name=\"talk-form\"", "</form>");
103  
  
104  
  //html = html.replace("PUT # OF BOTS HERE", loaded + "/" + total);
105  
  
106  
  L history = cast get(mainBot, "history");
107  
  new StringBuffer buf;
108  
  pcall {
109  
    int x = max(l(history)-5, 0);
110  
    print("l(history)=" + l(history) + ", x=" + x);
111  
    for (int i = l(history)-1; i >= x; i--) {
112  
      O dialog = history.get(i);
113  
      S q = shorten(getString(dialog, "question"), maxQuestionLength);
114  
      S a = shorten(getString(dialog, "answer"), maxAnswerLength);
115  
      buf.append(htag("li", "User: <b>" + htmlencode(q) + "</b>", "class", "splash-li") + "\n");
116  
      buf.append(htag("li", system() + ": <b>" + htmlencode(a) + "</b>", "class", "splash-li") + "\n");
117  
    }
118  
  }
119  
  
120  
  L<S> tok = htmlcoarsetok(html);
121  
  
122  
  // Last dialogs
123  
  
124  
  int i1 = tok.indexOf("<ul class=\"splash-ul\">");
125  
  int i2 = indexOf(tok, "</ul>", i1+1);
126  
  //print("i1=" + i1 + ", i2= " + i2);
127  
  if (i1 >= 0 && i2 > i1) {
128  
    clearAllTokens(tok.subList(i1+1, i2));
129  
    tok.set(i1+1, str(buf));
130  
  }
131  
  
132  
  // News
133  
  
134  
  i1 = indexOf(tok, "<ul class=\"splash-ul\">", i1+1);
135  
  i2 = indexOf(tok, "</ul>", i1+1);
136  
  if (i1 >= 0 && i2 > i1) {
137  
    clearAllTokens(tok.subList(i1+1, i2));
138  
    S dehnews = news;
139  
    pcall {
140  
      S score = or((S) callOpt(getBot("#1002408"), "getTuringScore"), "not calculated yet");
141  
      dehnews = dehnews.replace("$turing", score);
142  
    }
143  
    tok.set(i1+1, dehnews);
144  
  }
145  
  
146  
  O visitorsBot = callOpt(mainBot, "getBot", "#1002157");
147  
  S visitorStats = visitorsBot == null ? null : str(callHtmlMethod(visitorsBot, "/"));
148  
  
149  
  if (nempty(visitorStats)) pcall {
150  
    //html = "<!--\n" + visitorStats + "\n-->\n" + html;
151  
    
152  
    // Coolbox
153  
    
154  
    visitorStats = visitorStats.substring(visitorStats.indexOf("<br>") + 4);
155  
  
156  
    int i = tok.indexOf("</center>");
157  
    if (i < 0) i = tok.indexOf("<div class=\"boxcontainer\">");
158  
    if (i >= 0)
159  
      tok.set(i, tok.get(i) +
160  
    [[
161  
      <div class="coolbox">
162  
        <div class="boxtitle"><h1>$title</h1></div>
163  
        <div class="boxcontent">$content</div>
164  
      </div>
165  
    ]].replace("$title", "Visitor Stats")
166  
      .replace("$content", htag("p", visitorStats)));
167  
  }
168  
  
169  
  html = join(tok);
170  
  
171  
  html = html.replace("(latest first)", "(latest first, " + l(history) + " total)");
172  
  
173  
  // Bot List link
174  
  
175  
  S botsList = "/1002771";
176  
  html = html.replace("=\"http://bots.tinybrain.de/\"" , "=\"" + botsList + "\"");
177  
  html = html.replace("=\"http://bots.tinybrain.de\"", "=\"" + botsList + "\"");
178  
  
179  
  ret html;
180  
}
181  
182  
static S encodeAnswerForWeb(S s) {
183  
  s = s.trim();
184  
  
185  
  new Matches m;
186  
  S s2 = dropPrefixIgnoreCase("lookie here: ", s);
187  
  if (neq(s, s2) || s.contains(".gstatic.com/images?") || s.startsWith("http://tinyurl.com/")) {
188  
    S url = s2.trim();
189  
    print("url: " + quote(url));
190  
    if (isURL(url))
191  
      ret "<br>" + tag("img", "", "src", url);
192  
  }
193  
  
194  
  if (isURL(s))
195  
    ret tag("a", htmlencode(s), "href", s, "target", "_blank");
196  
197  
  new StringBuilder buf;
198  
  int i = 0;
199  
  while (i < l(s)) {
200  
    int i1 = smartIndexOf(s, "```", i);
201  
    int i2 = smartIndexOf(s, "```", i1+3);
202  
    buf.append(htmlencode(s.substring(i, i1)));
203  
    if (i2 > i1)
204  
      buf.append(sourceCodeToHTML(s.substring(i1+3, i2)));
205  
    i = i2+3;
206  
  }
207  
  ret str(buf);
208  
}

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: 1076 / 9168
Version history: 2 change(s)
Referenced in: [show references]