Welcome Guest, Not a member yet? Register   Sign In
proprietary tags in text for parsing and converting to html tags
#1

[eluser]Andy UK[/eluser]
I'm considering allowing my users to add proprietary tags to a text input to show titles, lists, etc. I would like to do something like is used in this forum with the code, quote, bold tags and such tags.

It's not really a template system as such. It would only apply to one text box which i would have to parse before sending to the view to swap the proprietary tags for html and in the case of lists, add and <li> for each new line.

Has anyone had any experience doing this? What's a good way of enclosing proprietary tags.... Perhaps double brackets like here. Any tips as to the best way to implement this would be welcome.

Thanks!
#2

[eluser]treenef[/eluser]
I guess you're trying to write your own rich text editor. As a bit of advice I would say don't bother. Go for one already out there and save yourself the hassle.
#3

[eluser]Andy UK[/eluser]
The thing is, I don't want to give users that many options. It would only be for lists, one type of title, em and pull quotes. I need to keep everything as simple as possible, very much like in this forum
#4

[eluser]treenef[/eluser]
Well, yeah you don't need a full blow text editor. There are some that are stripped down. Worse case scenario you just disable the buttons you don't need.

http://www.sitepoint.com/html5-wysiwyg/

Look through the list and pick one that suits your fancy.
#5

[eluser]Andy UK[/eluser]
Ok,

And if for arguments sake I wanted to keep things in-house and just add a bit of code rather than loading a third party text editor? Are we talking regex for swapping tags for entities? I mean it's going to be about as basic as you can get.

[title] maps to <h2>
[pullquote] maps to <span class="pullquote">
[bold] maps to <em>

and the only one that requires a bit more thought is
  • which maps to <ul> but with the addition of <li>s for each new line.
#6

[eluser]treenef[/eluser]
No not at all. Unless you've tried this for yourself, which I have btw, you don't know what can of worms you are opening.

Imagine you've got NESTED bbcodes. So codes within codes to parse. Quickly your regex become unmanageable. What happens if the user forgets a closing tag?

Personally, what I've done is to use wordpress's shortcodes as my own parsing engine. It works quite nicely and allows nested bbcodes.

https://github.com/WordPress/WordPress/b...tcodes.php

You'll also want to prevent javascript attacks. If the user types in (script) alert('some_redirect'); (/script)

It gets tricky real fast... but I guess you could use codeigniter's xss clean as an idea.
#7

[eluser]Andy UK[/eluser]
Thanks treenef.

I wasn't aware of bbcodes, so that's helped me get some more interesting results from Google straight away. To put things in context, this is for an in-house system with very few users with password access only. Basically one or two people are going to be using it to input property and project details and i wanted to give them a bit more freedom as to adding lists and subtitles and the odd pullquote. This is then fed to a specific template which is the same for all publications.

There won't be any nested bbcode and it's only applicable to a single textarea which in turn is just one section of a larger template. Main titles and such are inputed in their own fields. Basically, all users are already trusted and if they wanted to do anything malicious, there are many other ways to do it that don't involve injecting code.

I'll check out Wordpress shortcodes, but that still might be overkill for what i'm trying to do. Do you have any other observations now that the application is clearer?

Many thanks for your time
#8

[eluser]treenef[/eluser]
Hmm, just a quick web search the best option which seems to cover all your bases is probably scribe.

https://github.com/guardian/scribe

http://guardian.github.io/scribe/
#9

[eluser]Andy UK[/eluser]
Scribe looks like a nice solution. Very clean html output. I'm still trying to figure out how to install it on my hosting as i've not come across Bower before and don't have shell access either. Also, the documentation is very sparse. It's going to be a bit of a guessing game to get it working.

Shame there's no tutorial available as it seems to be a great option. I'm not at all familiar with Bower, npm, AMD and the rest of it. It's not even clear how the data is stored, if it converts to html and then stores or if it is stored with custom markup.

#10

[eluser]Andy UK[/eluser]
No joy whatsoever trying to get Scribe working, so i'm going back to bbcode. Two questions for anyone who has done this:

1. Should i store the bbcode text in the database text field and convert to html when rendering the public view or is it better to convert and then store just html in the db. With the second i would have to convert back to bbcode when editing the text.

2. Does anyone know a good way to manage lists? From what i've seen online, bbcode requires list items to be prefixed. It seems to me it would be so much easier just to open and close a list and have list items determined by new lines. Something like this:
  • Item one
    Item two
    Item three

I guess i could always stick with standard bbcode and do this, but it seems like an unnecessary additional step:
  • * Item one
    * Item two
    * Item three

I much prefer the first method as it's more intuitive for those who have to input the data.

I can figure out the regex for the basic stuff where it's just a case of swapping text for <em>text</em>, but i could do with some help with the list conversion from bbcode to html and back again. Unless of course it's recommended to store bbcode 'n' all in the db, in which case i wouldn't have to ever convert back from html to bbcode.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB