Giter Site home page Giter Site logo

comiclandapp / ashton Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ideasoncanvas/ashton

0.0 0.0 0.0 953 KB

Converts NSAttributedStrings into HTML (and back)

License: MIT License

Ruby 0.84% Objective-C 0.51% HTML 6.93% Swift 87.16% Rich Text Format 4.56%

ashton's Introduction

Ashton

Carthage Compatible Platforms iOS, macOS Language Swift License Build status Twitter: @mschwarz

Ashton (AttributedStringHTMLTransformation) is an iOS and macOS library for fast conversion of NSAttributedStrings into HTML, and back. Ashton is battle-tested and used in MindNode, for persisting formatted strings.

2.0 Release

The latest release is a complete rewrite in Swift focusing on improved performance and functional backwards compatibility to Ashton 1.x. The new codebase has a comprehensive test suite with a test coverage of > 90% and additional tests against the legacy 1.0 output.

Find out more about the launch of Ashton 2.0 in our Blog Post.

Supported Attributes

The following NSAttributedString.Key attributes are supported, when converting to HTML:

  • .backgroundColor (persisted as RGBA)
  • .foregroundColor (persisted as RGBA)
  • .underlineStyle (single, double, thick)
  • .underlineColor (persisted as RGBA)
  • .strikethroughColor (persisted as RGBA)
  • .strikethroughStyle (single, double, thick)
  • .font
  • .paragraphStyle (text alignment)
  • .baselineOffset
  • NSSuperScript
  • .link

Supported HTML Tags & Attributes

As Ashton supports only tags which are necessary to persist the attributes mentioned above, not all HTML tags are supported when converting HTML --> AttributedString. Basically, Ashton converts an AttributedString into a concatenation of span, p and a tags with style attributes.

Supported HTML Tags:

  • span
  • p
  • a
  • em
  • strong

The following style attribute keys are supported:

  • background-color
  • color
  • text-decoration
  • font
  • text-align
  • vertical-align
  • Additional custom attributes (-cocoa-strikethrough-color, -cocoa-underline-color, -cocoa-baseline-offset, -cocoa-vertical-align, -cocoa-font-postscriptname, -cocoa-underline, -cocoa-strikethrough, -cocoa-fontFeatures)

Colors have to be formatted as rgba like rgba(0, 0, 0, 1.000000).

Integration

Carthage

Add this line to your Cartfile.

github "IdeasOnCanvas/Ashton"

Integration with the Swift Package Manager

The Swift Package Manager is a dependency manager integrated with the Swift build system. To learn how to use the Swift Package Manager for your project, please read the official documentation.
To add ZIP Foundation as a dependency, you have to add it to the dependencies of your Package.swift file and refer to that dependency in your target.

// swift-tools-version:5.1
import PackageDescription
let package = Package(
    name: "<Your Product Name>",
    dependencies: [
		.package(url: "https://github.com/IdeasOnCanvas/Ashton/", .upToNextMajor(from: "2.0.0"))
    ],
    targets: [
        .target(
		name: "<Your Target Name>",
		dependencies: ["Ashton"]),
    ]
)

After adding the dependency, you can fetch the library with:

$ swift package resolve

Usage

Encode HTML

let htmlString = Ashton.encode(attributedString)

Decode NSAttributedString

let attributedString = Ashton.decode(htmlString)

Example App

An example app can be found in the /Example directory. It can be used to test NSAttributedString -> HTML -> NSAttributedString roundtrips and also to extract the HTML representation of an `NSAttributedString.

Credits

Ashton is brought to you by IdeasOnCanvas GmbH, the creator of MindNode for iOS, macOS & watchOS.

ashton's People

Contributors

apparentsoft avatar aquarius avatar dependabot[bot] avatar gurpartap avatar hannesoid avatar indragiek avatar msch avatar myell0w avatar patrick-kladek avatar schwmi avatar weichsel 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.