GithubHelp home page GithubHelp logo

hyeonjae / bumblebee Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daltoniam/bumblebee

0.0 3.0 0.0 190 KB

Abstract text processing and pattern matching engine in Swift. Converts text into NSAttributedStrings. Builtin markdown support.

License: Apache License 2.0

Ruby 8.65% Swift 84.97% Objective-C 6.38%

bumblebee's Introduction

bumblebee

Bumblebee is an abstract text processing and pattern matching engine in Swift for iOS and OSX. This provides support for things like markdown and basic HTML tags to be properly converted from raw text to expected style using NSAttributedString. Example markdown engine is included.

Features

  • Abstract and simple. Creating patterns is simple, yet flexible.
  • Fast. Only one pass is make through the raw string to minimize parse time.
  • Simple concise codebase at just a few hundred LOC.

Examples

First thing is to import the framework. See the Installation instructions on how to add the framework to your project.

import Bumblebee

This is a simple code example, but showcases a powerful use case.

//first we create our label to show our text
let label = UILabel(frame: CGRectMake(0, 65, view.frame.size.width, 400))
label.numberOfLines = 0
view.addSubview(label)

//we create this textAttachment to show our embedded image
var textAttachment = NSTextAttachment(data: nil, ofType: nil)

//the raw text we have.
let rawText = "Hello I am *red* and I am _bold_. Here is an image: ![](http://vluxe.io/assets/images/logo.png)"

//create our BumbleBee object.
let bee = BumbleBee()

//our red text pattern
bee.add("*?*", recursive: false) { (pattern: String, text: String, start: Int) -> (String, [NSObject : AnyObject]?) in
    let replace = pattern[advance(pattern.startIndex, 1)...advance(pattern.endIndex, -2)]
    return (replace,[NSForegroundColorAttributeName: UIColor.redColor()])
}
//the bold pattern
bee.add("_?_", recursive: false) { (pattern: String, text: String, start: Int) -> (String, [NSObject : AnyObject]?) in
    let replace = pattern[advance(pattern.startIndex, 1)...advance(pattern.endIndex, -2)]
    return (replace,[NSFontAttributeName: UIFont.boldSystemFontOfSize(17)])
}
//the image pattern
bee.add("![?](?)", recursive: false, matched: { (pattern: String, text: String, start: Int) in
    let range = pattern.rangeOfString("]")
    if let end = range {
        let findRange = pattern.rangeOfString("(")
        if let startRange = findRange {
            let url = pattern[advance(startRange.startIndex, 1)..< advance(pattern.endIndex, -1)]
			//using Skeets, we can easily fetch the remote image
            ImageManager.sharedManager.fetch(url, progress: { (Double) in
                }, success: { (data: NSData) in
                    let img = UIImage(data: data)
                    textAttachment.image = img
                    textAttachment.bounds = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
                    label.setNeedsDisplay() //tell our label to redraw now that we have our image
                }, failure: { (error: NSError) in
            })
        }
        return (bee.attachmentString,[NSAttachmentAttributeName: textAttachment]) // embed an attachment
    }
    return ("",nil) //don't change anything, not a match
})
//now that we have our patterns, we call process and get the NSAttributedString
let attrString = bee.process(rawText)
label.attributedText = attrString

Which looks like:

example

Image Loading Library: Skeets

Details

The ? is the wildcard. It is simply means that any character between these opening and closing characters could be a match.

Requirements

Bumblebee requires at least iOS 7/OSX 10.10 or above.

Installation

Cocoapods

Check out Get Started tab on cocoapods.org.

To use Bumblebee in your project add the following 'Podfile' to your project

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Bumblebee', '~> 0.9.1'

Then run:

pod install

Carthage

Check out the Carthage docs on how to add a install. The Bumblebee framework is already setup with shared schemes.

Carthage Install

Rogue

First see the installation docs for how to install Rogue.

To install JSONJoy run the command below in the directory you created the rogue file.

rogue add https://github.com/daltoniam/bumblebee

Next open the libs folder and add the Bumblebee.xcodeproj to your Xcode project. Once that is complete, in your "Build Phases" add the Bumblebee.framework to your "Link Binary with Libraries" phase. Make sure to add the libs folder to your .gitignore file.

Other

Simply grab the framework (either via git submodule or another package manager).

Add the Bumblebee.xcodeproj to your Xcode project. Once that is complete, in your "Build Phases" add the Bumblebee.framework to your "Link Binary with Libraries" phase.

Add Copy Frameworks Phase

If you are running this in an OSX app or on a physical iOS device you will need to make sure you add the Bumblebee.framework or BumblebeeOSX.framework to be included in your app bundle. To do this, in Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar. In the tab bar at the top of that window, open the "Build Phases" panel. Expand the "Link Binary with Libraries" group, and add Bumblebee.framework or BumblebeeOSX.framework depending on if you are building an iOS or OSX app. Click on the + button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and add Bumblebee.framework or BumblebeeOSX.framework respectively.

TODOs

  • Complete Docs
  • Add Unit Tests
  • Create full markdown engine example.

License

Bumblebee is licensed under the Apache v2 License.

Contact

Dalton Cherry

bumblebee's People

Contributors

acmacalister avatar daltoniam avatar matteocrippa avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.