Jump to content
Science Forums

Recommended Posts

Posted

OK, I am learning how to do web development, and I would like to enable smilies in documents people post on my site. So that drives me to the question...

 

How do smilies work? :shrug: :eek2:

 

What are the tricks? What do I need to include in my site to enable them? I am using asp.net 2.0

 

Thanks,

 

Bill

Posted

You need to have the smilies as images in your site.

 

When you display a post, you have to parse it for the smilie codes, and where you find them, throw up the image.

 

You can do parsing the simple way or the hard way (using vBulletin codes):

 

Simple: Look for colons, then look for the next one and match the letters in between to your list of smilies, if you find one, throw away the stuff between the two colons and put up the image, otherwise print the text.

 

Hard: Learn to use Lex and Yacc (and of course figure out how to use them in VB.Net! :eek2: )

 

Easy as pie,

Buffy

Posted

Thanks Buff

 

Sounds pretty straight forward. Of course the trick is always making "easy" actually seem easy. I will let you know how it turns out.

 

If anyone else has any suggestions I am still listening. :eek2:

 

Bill

Posted

Another question, Buffy...

 

In mssql I can have an image as a field. So hypothetically, I could create a table of my smilies with one column containing the text indicator, and another column containing the image. I could have a function that searches posts by rattling through the table and looking for matching text in the target post. I could wrap it up nicely. The question is should I use the Image datatype in the table, or keep the images in a folder and just keep the path to the images in the table? Is there a best practice for this?

 

Bill

Posted

The problem is that you're displaying them in a web page, and the web page wants a reference to a file. If you put it in the db, then you're going to at least have to create a file from the image in the db, and if you do that on the fly, you have all sorts of fun with creating and maintaining temp files (good luck, Mr. Phelps). If you want to do this in the db (and you might do better with an array of objects that is statically defined in your code, unless you're going to be adding and deleting them all the time), then your table should just have columns for the smilie code string and the path to the related file as a string (and any other classification garbage you might want to add).

 

select path from smilies where code = ':evil:',

Buffy

Posted
The reason I am going with the DB is so I can build a utility for adding/removing smilies that does not require opening any code.
Yep, that's why you do it. Most programs have some that are dynamic and some that are static, and you should freely use both as appropriate: the db method is always much slower...

 

Interprocess communications overhead,

Buffy

Posted

you dont modify any code TBD, what you do is you store what is supposed to become a smiley in one field, and a link to a location in another, in your code you do a string search and replace where the word is equal to what smileys you have in that first field, and replace that with a location of the image...

Posted

... surrounded by an img tag!

 

Apart from this nitpicking I agree with Alex, it would make no sense at all to use a special field type for images and keep converting these into a file. You need the file at each HTTP response.

 

Look for colons, then look for the next one and......
:cup:

 

And the same for :). The real reason for the : characters is just to make it less likely for posters to accidentally hit a code when it ain't their intention. This is a stickier problem than in a programming language, you can expect a developers to watch out, and get their escape sequences right, and correct them if they've botched it, but you want it to be slick for the end users. Nevertheless, one would rarely type :foo_bar in English or other ordinary languages.

 

I think all smiley codes begin with a colon and contain no spaces so you should perhaps look for a space after having found a colon but watch for other possible delimiters. Perhaps any delimiter excepting underscore? Surely there is some spec, since folks are beginning to expect standard ones being useable. If you really don't care about adding more smileys without having to re-code, do what's OK for the coses you want. Of course, one could always do something based on current max length of the codes... :cup:

Posted

Actually, the max length idea is simple if you let the DBMS do it for you although still potentially heavy:

IF Get(cur_pos) = ':' THEN -Could be start of a smiley
str = LookForward(MAX_LENGTH)
sql = "SELECT smiley_path FROM x WHERE '" & str
sql = sql & "' LIKE smiley_code + '%' AND..."
rs.Open sql
DoIt rs("smiley_path")
ENDIF

The unusual sql syntax for LIKE works on Oracle (apart from || insead of the +). Of course it's up to the codes to be univocal.

 

An idea to reduce sweat at each HTTP response would be for each smiley to have a user code of the type :sheeeeesh: and also an internal numeric one that's less mnemonic but more standard; translation would occur less often than every view.

 

:cup: I was forgetting about :), no colon at all...

 

P. S. you'd need the query to also return the length of the code in order to know where to resume parsing from.

Posted

WOW!! I never stop being more and more impressed with everyone here at Hypo.

 

I think that in .net I have some tools that will make this easy. Using String.IndexOf() I can find the starting position of the first occurance of the string I am searching for. For each one that I find I replace with the image tagged string, and then resume the search. I repeat this until each smiley has come up blank. This gives me the start point of each exact match of my search. I don't need to keep track of where I am searching because I only find matches.

 

Now, the next step is to get the time to write it...

 

Bill

Posted

A couple of things come to mind from Q's post above:

  • Looking for codes in random text is not like traditional parsing: my reference to lex and yacc was a bit tongue in cheek, but only sort of, because you have to understand the concept of "lookahead" that you learn in Compiler class. So the "look for a colon" then "look for another colon" is actually practical, because you're only going to either find a smiley code (see caveat below) and throw out your lookahead buffer, or you're not and you're going to output the whole buffer.
  • I didn't bother to think about the non-colon delimited smilies, which will get you into some really interesting parsing issues. "Easy" way is to have a different parsing routine for any smilie that is not colon-delimited. The brute force way to do this is create an array of your non-colon-delimited smilies, along with another array of just the unique first characters that can trigger them. As you are parsing, you need to check every character against the latter array, and then if there's a match, start stuffing the characters into your lookahead buffer, and for each character added, look for an exact match with the former array. You can terminate the lookahead as soon as you've gotten past the length of the longest string in the former array. Great way to exercize that 3Ghz processor!
  • DB sproc languages (Oracle and MSSQL) are totally sucky. When you have .Net's String library, this kind of parsing is great because the text is just a blob, and it handles all the memory management for you. In DBsprocs and with traditional programming language datatypes (e.g. char[n] in C/C++), you have to be painfully aware of how big your data variables are.

Have fun with it over the holidays, Big!

 

try {Convert::ToInt64(i);} catch (FormatException ^fe) { Console::Write("You meathead, that wasn't an integer!"); },

Buffy

Posted

i know you can do it in php with str_replace

first array of smileys, second array of replace values, and then str_replace(from_what, to_what, str)

 

i also did it with an associative array, but i dont remember how, however the 2 array method seems like it would work better in your case of using a database... you will have to cross reference with asp for an str_replace-like function.

Posted
i know you can do it in php with str_replace

first array of smileys, second array of replace values, and then str_replace(from_what, to_what, str)

PHP has some pretty cool features! But also, I have spent too many years with C/C++ and lex, so I forget about cool routines like "replace" (not in <strings.h>!!!): its in vb.NET, so proggy well, my droogies. Automates all that lookahead buffer stuff I mention above...
i also did it with an associative array, but i dont remember how, however the 2 array method seems like it would work better in your case of using a database... you will have to cross reference with asp for an str_replace-like function.
Of course in a sense, a relational database *is* an associative array, but its much cooler if its part of the language (Lisp!)...

 

Lexical Analysis the Old Fashioned Way,

Buffy

Posted
try {Convert::ToInt64(i);} catch (FormatException ^fe) { Console::Write("You meathead, that wasn't an integer!"); }Buffy
Where the meathead is that poor poster that durst type something like :aaargh: into a post. :0005:

 

That ain't exactly what I was thinkin' of, in my musings about perf... However, doubting Bill would be overly concerned about handling large traffic I suggested simplifying things with the select statement but, if VB.NET has a such neat replace function with array params like that, all the better for Bill!

Posted
Where the meathead is that poor poster that durst type something like :aaargh: into a post. :oh_really:

 

That ain't exactly what I was thinkin' of, in my musings about perf... However, doubting Bill would be overly concerned about handling large traffic I suggested simplifying things with the select statement but, if VB.NET has a such neat replace function with array params like that, all the better for Bill!

Here is an update for y'all.

 

I have started by inserting <br> tags into the text of posts in place of chr(13). Actually I am replacing (in vbese) ControlChars.crlf whick is both chr(10 & chr(13). I built a function that finds these and replaces them. Next I am building a similar function that will scan for smilies and make the appropriate find/replace.

 

On the DB side I have decided to have two fields for posts. One I call the Post, and the other is the RawPost. The RawPost is the text sent up to the server by the user. I take it and run it through all the find replace routines. Then I update the DB with both the raw and the processed. In fields where I want to present the post I link to the Post field. In controls used for editing I link to the RawPost field. This uses more disk space, but saves me from having to translate the posts on the fly each time they are put into a control for viewing.

 

My next step is to make my first smilie and get it working. Then I can add them at will. I will give a link to the final product when it is ready.

 

Bill

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...