Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Put up code and README

  • Loading branch information...
commit 9247077e8da4e25fab3d8927bbbd44fb12762cd5 0 parents
@ctcherry authored
Showing with 73 additions and 0 deletions.
  1. +8 −0 README.md
  2. +65 −0 jquery.wrap-first-letter.js
8 README.md
@@ -0,0 +1,8 @@
+jQuery Wrap First Letter
+========================
+
+Usage
+-----
+
+- Include it in site
+- $('p:first').wrapFirstLetter();
65 jquery.wrap-first-letter.js
@@ -0,0 +1,65 @@
+(function($){
+
+ $.fn.wrapFirstLetter = function(options) {
+
+ var settings = {
+ 'charpattern' : /[a-z0-9]/i,
+ 'tag' : 'span',
+ 'class' : 'first-letter'
+ };
+
+ if (options) {
+ jQuery.extend(true, settings, options);
+ }
+
+ function _wrap(content) {
+ var len = content.length;
+ var intag = false;
+
+ for (var i=0; i<len; i++) {
+ if (content[i] == '<') {
+ intag = true;
+ continue;
+ }
+
+ if (content[i] == '>') {
+ intag = false;
+ continue;
+ }
+
+ if (intag) {
+ continue;
+ }
+
+ if (settings.charpattern.test(content[i])) {
+ var pre = '';
+ var post = '';
+ var t = settings.tag;
+ var c = settings.class;
+ if (i==0) {
+ post = content.substring(1);
+ return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
+ }
+ if (i==(len-1)) {
+ pre = content.substring(0,i);
+ return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
+ }
+
+ pre = content.substring(0,i);
+ post = content.substring(i+1);
+ return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
+ }
+ }
+ }
+
+
+ return this.each(function() {
+ var obj = $(this);
+ var content = obj.html();
+ var newcontent = _wrap(content);
+ obj.html(newcontent);
+ });
+
+ };
+
+})(jQuery);

0 comments on commit 9247077

Please sign in to comment.
Something went wrong with that request. Please try again.