Coder Social home page Coder Social logo

nysander / twitter-text Goto Github PK

View Code? Open in Web Editor NEW
58.0 4.0 8.0 1.02 MB

Swift implementation of twitter-text library

Home Page: https://nysander.github.io/twitter-text/

License: MIT License

Swift 99.67% Shell 0.33%
swift swift5 spm twitter twitter-api idn unicode-domain unicode

twitter-text's Introduction

TwitterText logo

MIT License Swift 5.1 Github Actions


This is the Swift implementation of the twitter-text parsing library. The library has methods to parse Tweets and calculate length, validity, parse @mentions, #hashtags, URLs, and more.

Setup

Add twitter-text directly to your Package.swift file:

dependencies: [
    // ...
    .package(url: "https://github.com/nysander/twitter-text.git", from:"0.0.1"),
],
targets: [
.target(name: "Your App", dependencies: [
    /// ...
    .product(name: "twitter-text", package: "twitter-text"),
]),

or via Xcode:

search for https://github.com/nysander/twitter-text.git repository

Xcode setup

Conformance tests

To run the Conformance test suite from the command line:

% swift test --enable-test-discovery

You can also run the tests from within Xcode itself. Open the project file and run the tests are you normally would (Cmd-U).

API

twitter-text 2.0 introduces configuration files that define how Tweets are parsed for length. This allows for backwards compatibility and flexibility going forward. Old-style traditional 140-character parsing is defined by the v1.json configuration file, whereas v2.json is updated for "weighted" Tweets where ranges of Unicode code points can have independent weights aside from the default weight. The sum of all code points, each weighted appropriately, should not exceed the max weighted length.

Some old methods from twitter-text 1.0 have been marked deprecated, such as the various +tweetLength: methods. The new API is based on the following method, -parseTweet:

- TwitterTextParser parseTweet(text:)

This method takes a string as input and returns a results object that contains information about the string. TwitterTextParseResults includes:

  • weightedLength: Int: the overall length of the tweet with code points weighted per the ranges defined in the configuration file.

  • permillage: Int: indicates the proportion (per thousand) of the weighted length in comparison to the max weighted length. A value > 1000 indicates input text that is longer than the allowable maximum.

  • isValid: Bool: indicates if input text length corresponds to a valid result.

  • displayTextRange: NSRange: An array of two unicode code point indices identifying the inclusive start and exclusive end of the displayable content of the Tweet. For more information, see the description of display_text_range here: Tweet updates

  • validDisplayTextRange: NSRange: An array of two unicode code point indices identifying the inclusive start and exclusive end of the valid content of the Tweet. For more information on the extended Tweet payload see Tweet updates

Issues

Have a bug? Please create an issue here on GitHub!

https://github.com/nysander/twitter-text/issues

Authors

Source of inspiration

This library was directly based upon its Objective-C implementation which can be found in twitter/twitter-text repository, written by Satoshi Nakagawa, David LaMacchia and Keh-Li Sheng.

Version 3.1.0 was used as reference. All test case JSON files used in this library are directly copied from said repository to keep consistency and being sure that results are the same.

License

Copyright 2020 Paweł Madej, and other contributors

Licensed under the MIT License

twitter-text's People

Contributors

kyome22 avatar nskonieczko avatar nysander avatar ricky-sharp avatar rizwankce avatar stephenwoodford avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

twitter-text's Issues

Entity protection level

I have installed twitter-text using SPM and I can't access type and range for Entity for TwitterText.

Can we make the proprieties for Entity public as well?

'type' is inaccessible due to 'internal' protection level.
'range' is inaccessible due to 'internal' protection level.

Parser crash

Instantiating defaultParser on real device crashes. The fix I did is the following, plus instantiating via setDefaultParser(with: Configuration)

private static var _defaultParser: Parser?
    public static var defaultParser: Parser {
      get {
        if _defaultParser == nil {
          _defaultParser = Parser(with: Configuration.configuration(fromJSONResource: ConfigurationType.v3)!)
        }
        return _defaultParser!
      }
      set {
        _defaultParser = newValue
      }
    }

Understanding on how to implement

I'm trying to understand how to implement this into either a SwiftUI view or a UIKit view.

I'm tried calling Text(TwitterText.entities(in: myText)) and no luck with the error No exact matches in call to initializer

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.