Returning Rendered HTML from Chatbot - javascript

I am using the chatbot from liouh (https://github.com/liouh/chat-bot). When I attempt to return a URL, it gives me unrendered/raw HTML instead of a link. How can I return a rendered link given the Google scenario below? Thank you
function chatBot() {
// current user input
this.input;
this.respondTo = function(input) {
this.input = input.toLowerCase();
if(this.match('link to google'))
return "<a href='http://google.com'>Google.com</a>";
if(this.input == 'noop')
return;
return "I dont know the answer to '" + input + "'. You can teach me that via the link at the top of this page.";
}
this.match = function(regex) {
return new RegExp(regex).test(this.input);
}
}

It looks like in line 54 of index.js you need to change the code from
line.find('.text').text(text);
to
line.find('.text').html(text);
this will append the incoming reply as HTML instead of plain text.

Related

Javascript variable string replacement

Another stupid question.
I believe I understand this right but it doesn't seem to work.
function parseCustomCommands($text, $textParts) {
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
switch($textParts[0]) {
case '/takeover':
$this->insertChatBotMessage( $this->getChannel(), $text );
return true;
default:
return false;
}
}
}
ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
case '/takeover':
text=text.replace('/takeover', ' ');
return '<span class="chatBotMessage">' + text + '</span>';
default:
return text;
}
}
It's executed when /takeover is this sent and the way I'm looking at it the '/takeover' part is meant to be replaced with nothing leaving just the . This does not seem to be the case..
Is anyone able to point out the mistake in it? I've tried several things with $ in variable names and using different variables to remove it.
$re="/takeover";
$str=text.replace(re, ' ');
I've tried too.
Thanks in advance.
This is really a PHP question
github source
Review the other "insertParsedMessage*" PHP methods
I'm not a PHP developer but it should be something like this based on the documentation
$text = str_replace("/takeover","", $text);

Adding "no results" into search/filter script

yea I know ... another "no results" question ... sorry for that, but i need to ask because i'm fighting with it couple of hours now and preety much nothing works.
I got some script to filter items and I need your help to inject some code to display "no results" message.
$("#my-search-input").keyup(function() {
var search = $(this).val();
$(".my-list").children().show();
if (search)
$(".my-list").children().not(":containsNoCase(" + search + ")").hide();
});
$.expr[":"].containsNoCase = function (el, i, m) {
var search = m[3];
if (!search) return false;
return new RegExp(search,"i").test($(el).text());
};
Here's my fiddle:
http://jsfiddle.net/bk13detv/22/
Basically message should display in every instance of table.
Thanks for your time, cheers.
You could try something like this:
$("#my-search-input").keyup(function () {
var search = $(this).val();
$(".my-list").children().show();
$('.noresults').remove();
if (search) {
$(".my-list").children().not(":containsNoCase(" + search + ")").hide();
$(".my-list").each(function () {
if ($(this).children(':visible').length == 0)
$(this).append('<tr class="noresults"><td colspan="3"><em>No Results</em></td></tr>');
});
}
});
See this Fiddle for a demo.

How to add custom image tag to pagedown?

I'm attempting to duplicate the original img tag's functionality in custom img tag that will be added to the pagedown converter.
e.g I'm copy the original behavior:
![image_url][1] [1]: http://lolink.com gives <img src="http://lolink.com">
into a custom one:
?[image_url][1] [1]: http://lolink.com gives <img class="lol" src="http://lolink.com">
Looking at the docs the only way to do this is through using the preblockgamut hook and then adding another "block level structure." I attempted doing this and got an Uncaught Error: Recursive call to converter.makeHtml
here's the code of me messing around with it:
converter.hooks.chain("preBlockGamut", function (text, dosomething) {
return text.replace(/(\?\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, function (whole, inner) {
return "<img src=" + dosomething(inner) + ">";
});
});
I'm not very experienced with hooks and everything so what would I do to fix it? Thanks.
UPDATE: found out that _DoImages runs after prespangamut, will use that instead of preblockgamut
Figured it out! The solution is very clunky and involves editing the source code because I am very bad at regex and the _DoImage() function uses a lot of internal functions only in the source.
solution:
All edits will be made to the markdown.converter file.
do a ctrl+f for the _DoImage function, you will find that it is named in two places, one in the RunSpanGamut and one defining the function. The solution is simple, copy over the DoImage function and related stuff to a new one in order to mimic the original function and edit it to taste.
next to DoImage function add:
function _DoPotatoImages(text) {
text = text.replace(/(\?\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writePotatoImageTag);
text = text.replace(/(\?\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writePotatoImageTag);
return text;
}
function writePotatoImageTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
var whole_match = m1;
var alt_text = m2;
var link_id = m3.toLowerCase();
var url = m4;
var title = m7;
if (!title) title = "";
if (url == "") {
if (link_id == "") {
link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");
}
url = "#" + link_id;
if (g_urls.get(link_id) != undefined) {
url = g_urls.get(link_id);
if (g_titles.get(link_id) != undefined) {
title = g_titles.get(link_id);
}
}
else {
return whole_match;
}
}
alt_text = escapeCharacters(attributeEncode(alt_text), "*_[]()");
url = escapeCharacters(url, "*_");
var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
title = attributeEncode(title);
title = escapeCharacters(title, "*_");
result += " title=\"" + title + "\"";
result += " class=\"p\" />";
return result;
}
if you look at the difference between the new _DoPotatoImages() function and the original _DoImages(), you will notice I edited the regex to have an escaped question mark \? instead of the normal exclamation mark !
Also notice how the writePotatoImageTag calls g_urls and g_titles which are some of the internal functions that are called.
After that, add your text = _DoPotatoImages(text); to runSpanGamut function (MAKE SURE YOU ADD IT BEFORE THE text = _DoAnchors(text); LINE BECAUSE THAT FUNCTION WILL OVERRIDE IMAGE TAGS) and now you should be able to write ?[image desc](url) along with ![image desc](url)
done.
The full line (not only the regex) in Markdown.Converter.js goes like this:
text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);
so check the function writeImageTag. There you can see how the regex matching text is replaced with a full img tag.
You can change the almost-last line before its return from
result += " />";
to
result += ' class="lol" />';
Thanks for the edit to the main post.
I see what you mean now.
It is a bit weird how it uses empty capture groups to specify tags, but if it works, it works.
It looks like you would need to add on an extra () onto the regex string, then specify m8 as a new extra variable to be passed into the function, and then specify it as class = m8; like the other variables at the top of the function.
Then where it says var result =, instead of class =\"p\" you would just put class + title=\"" + .......

Jquery: Copy text from a form to a DIV while checking the last letter

On my HTML form, users can enter their name.
Their name will then appear in a DIV as part of a book title.
The book title uses apostrophe 's (e.g. Amy's Holiday Album).
If the user enters a name which ends in a S, I don't want the apostrophe s to appear.
(e.g. it should be Chris' Holiday Album instead of Chris's Holiday Album).
I also only want this to occur if the form has a class of apostrophe. If this class does not exist, then the name should be copied as is without any apostrophe or 's'.
I know you can use slice() to get the last character of an element, so I thought I could combine this with an if statement. But it doesn't seem to work.
Here is JSFiddle
Here is my HTML:
<div><b class="title"></b> Holiday Album</div>
Here is my Jquery (1.8.3):
$(document).ready(function() {
$('.name').keyup(function() {
var finalname = text($(this).val());
var scheck = finalname.slice(-1);
var finaltitle;
if ($(".apostrophe")[0]) {
if (scheck == 's') {
finaltitle = finalname + "'";
}
else {
finaltitle = finalname + "'s";
}
$('.title').text(finaltitle);
}
else {
$('.title').text(finalname);
}
});
});
text method is not needed on
var finalname = $(this).val();
check fiddle
Use
var finalname = $(this).val();
instead of
var finalname = text($(this).val());
Simplified version
$(document).ready(function() {
//Code fires when user starts typing:
$('.name.apostrophe').keyup(function() {
if (this.value.indexOf("'s") != -1 ) {
$('.title').text(this.value.replace(/'s/g, "'"));
} else {
$('.title').text(this.value)
}
}); /* Capture Personalised Message */
});
This will also replace all occurrences of the 's with ' only.
Hope it helps!.

Django - Reverse Engineering the Admin site's "Add Foreign Key" button

TL;DR (Short synopsis):
I have recreated the admin "Add" button in my own project. However, when I hit "save" on the parent form, it is not recognizing the new select element.
Whole Story:
I have that functionality working in my own project... almost. I need help figuring out the last step. As it is now, I have a "+" button, I click it, a popup shows up, I add a new object, hit save, popup closes and that new item is now in my select box and selected - just like the admin page. However, when I hit save on this parent form, I get the error that I've selected an item not in the list. Of course, since the page has reloaded, my new item is part of the list and I just hit save again and it works. Of course, I need it to save on the first time!
The basic setup is my Parent model is called System and the foreign key model is called Zone. The Zone model lists how many zones a system has (1 zone,2 zones,10 zones, etc...)
OK, some code:
The "Add" link in the template of the parent form:
Add
In my New_Zone view, after saving the new zone, I check if the popup GET variable is 1, if so, return a javascript function. Here's the view:
...
if form.is_valid():
f = form.save(commit=False)
pk_value = f.numOfZones
form.save()
obj = Zone_Info.objects.get(numOfZones=pk_value)
if isPopup == "1":
return HttpResponse('<script>opener.closeAddPopup(window, "%s", "%s");</script>' % (escape(pk_value), escape(obj)))
...
And here is my Javascript (largely copied from the admin javascript:
function html_unescape(text) {
// Unescape a string that was escaped using django.utils.html.escape.
text = text.replace(/</g, '<');
text = text.replace(/>/g, '>');
text = text.replace(/"/g, '"');
text = text.replace(/'/g, "'");
text = text.replace(/&/g, '&');
return text;
}
function windowname_to_id(text) {
text = text.replace(/__dot__/g, '.');
text = text.replace(/__dash__/g, '-');
return text;
}
function showAddPopup(triggeringLink, pWin) {
var name = triggeringLink.id.replace(/^add_/, '');
href = triggeringLink.href;
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
win.focus();
return false;
}
function closeAddPopup(win, newID, newRepr) {
newID = html_unescape(newID);
newRepr = html_unescape(newRepr);
var name = windowname_to_id(win.name);
var elem = document.getElementById(name);
if (elem) {
if (elem.nodeName == 'SELECT') {
var o = new Option(newRepr, newID);
elem.options[elem.options.length] = o;
o.selected = true;
} else if (elem.nodeName == 'INPUT') {
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
elem.value += ',' + newID;
} else {
elem.value = newID;
}
}
} else {
var toId = name + "_to";
elem = document.getElementById(toId);
var o = new Option(newRepr, newID);
SelectBox.add_to_cache(toId, o);
SelectBox.redisplay(toId);
}
win.close();
}
I took a look at this question and it seems that I am doing precisely the same thing.
Any ideas on how to get that last step of getting the form to recognize the new select element?
Thanks!
I Figured it Out
The problem was what I was passing to my closeAddPopup javascript function. Essentially, I was passing garbage values.
Here's what I originally had in my New_Zone view (which didn't work):
...
if form.is_valid():
f = form.save(commit=False)
pk_value = f.numOfZones
form.save()
obj = Zone_Info.objects.get(numOfZones=pk_value)
if isPopup == "1":
return HttpResponse('<script>opener.closeAddPopup(window, "%s", "%s");</script>' % (escape(pk_value), escape(obj)))
...
It's a pretty stupid mistake on my part (clearly it's late). I was assigning f to the field numOfZones which I thought was the pk and sending that to the script.
Now, the working view looks like this:
if form.is_valid():
obj = form.save()
pk_value = obj.pk
if "_popup" in request.REQUEST:
return HttpResponse('<script>opener.closeAddPopup(window, "%s", "%s");</script>' % (escape(pk_value), escape(obj)))
Anyway... thanks to... well, Stackoverflow. I don't think I would have solved the problem without posting the question and rereading my code on stackoverflow.

Categories