Skip to content

Commit

Permalink
Merge pull request #36 from bmoliveira/35-fix-utf16-string-utils
Browse files Browse the repository at this point in the history
Solve #35
  • Loading branch information
bmoliveira authored Jan 21, 2019
2 parents e74fecd + 0f6e7ff commit 64bb17f
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 17 deletions.
45 changes: 45 additions & 0 deletions Example/MarkdownKitViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// MarkdownKitViewModel.swift
// Example
//
// Created by Bruno Oliveira on 21/01/2019.
// Copyright © 2019 Ivan Bruel. All rights reserved.
//

import Foundation
import MarkdownKit
import Alamofire

class MarkdownKitViewModel {

let markdownParser: MarkdownParser

fileprivate let testingURL: String

var markdownAttributedStringChanged: ((NSAttributedString?, Error?) -> ())? = nil

init(markdownParser: MarkdownParser,
testingURL: String = "https://raw.githubusercontent.com/apple/swift-evolution/master/proposals/0240-ordered-collection-diffing.md") {
self.markdownParser = markdownParser
self.testingURL = testingURL
}
}

extension MarkdownKitViewModel {
func parseString(markdownString: String) {
markdownAttributedStringChanged?(markdownParser.parse(markdownString), nil)
}

func requestTestPage() {
AF.request(testingURL).responseString { [weak self]response in
if let error = response.error {
self?.markdownAttributedStringChanged?(nil, error)
return
}

response.result.withValue { [weak self]markdownString in
self?.parseString(markdownString: markdownString)
}
}
}
}
32 changes: 28 additions & 4 deletions Example/Resources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="jBK-b1-aaD">
<device id="retina5_9" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand All @@ -12,7 +12,7 @@
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="MarkdownKit" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Expand All @@ -22,7 +22,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="VJS-nm-4fY">
<rect key="frame" x="0.0" y="44" width="375" height="734"/>
<rect key="frame" x="0.0" y="88" width="375" height="690"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
Expand All @@ -33,16 +33,40 @@
<constraints>
<constraint firstAttribute="trailing" secondItem="VJS-nm-4fY" secondAttribute="trailing" id="3Az-hP-Vyg"/>
<constraint firstItem="VJS-nm-4fY" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="A8o-ll-qcn"/>
<constraint firstItem="VJS-nm-4fY" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="gj1-GG-eOu"/>
<constraint firstItem="VJS-nm-4fY" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="Dqv-gm-VsB"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="VJS-nm-4fY" secondAttribute="bottom" id="h56-PQ-oO7"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="Slf-WF-9Dh">
<barButtonItem key="rightBarButtonItem" title="Source" style="done" id="AJZ-l6-6Yu">
<connections>
<action selector="userDidTapSwitch:" destination="vXZ-lx-hvc" id="izC-Ji-xuO"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="textView" destination="VJS-nm-4fY" id="MSR-hV-voH"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="604" y="-58.374384236453203"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="vnj-93-E3R">
<objects>
<navigationController id="jBK-b1-aaD" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="xyc-Pd-j0n">
<rect key="frame" x="0.0" y="44" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="pJr-Ge-VKw"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AeI-gI-Z3q" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-316" y="-58"/>
</scene>
</scenes>
</document>
77 changes: 66 additions & 11 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,87 @@ import UIKit
import MarkdownKit

class ViewController: UIViewController {

@IBOutlet fileprivate weak var textView: UITextView! {
didSet {
textView.delegate = self
textView.isScrollEnabled = false
}
}

// Example with custom font
// fileprivate let markdownParser = MarkdownParser(font: UIFont(name: "Product Sans", size: UIFont.systemFontSize)!)
fileprivate lazy var attributedStringFromResources: String = {
return NSLocalizedString("Markdown", comment: "").stringByDecodingHTMLEntities
}()

fileprivate lazy var viewModel: MarkdownKitViewModel = {
// Example with custom font
// fileprivate let markdownParser = MarkdownParser(font: UIFont(name: "Product Sans", size: UIFont.systemFontSize)!)
let parser = MarkdownParser()
parser.addCustomElement(MarkdownSubreddit())

let viewModel = MarkdownKitViewModel(markdownParser: parser)
viewModel.markdownAttributedStringChanged = { [weak self](attributtedString, error) in
if let error = error {
NSLog("Error requesting -> \(error)")
return
}

guard let attributedText = attributtedString else {
NSLog("No error nor string found")
return
}

self?.textView.attributedText = attributedText
}

return viewModel
}()

fileprivate var resourcesAction: UIAlertAction {
return UIAlertAction(title: "Resources",
style: .default,
handler: { [unowned self]_ in
self.viewModel.parseString(markdownString: self.attributedStringFromResources)
})
}

fileprivate var internetAction: UIAlertAction {
return UIAlertAction(title: "Internet",
style: .default,
handler: { [unowned self]_ in
self.viewModel.requestTestPage()
})
}

fileprivate var actionSheetController: UIAlertController {
let alert = UIAlertController(title: "Choose the Markdown source", message: nil, preferredStyle: .actionSheet)
alert.addAction(resourcesAction)
alert.addAction(internetAction)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
alert.dismiss(animated: true, completion: nil)
}))
return alert
}

fileprivate let markdownParser = MarkdownParser()
override func viewDidLoad() {
super.viewDidLoad()
markdownParser.addCustomElement(MarkdownSubreddit())
textView.attributedText = markdownParser.parse(NSLocalizedString("Markdown", comment: "").stringByDecodingHTMLEntities)
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = "MarkdownKit"
viewModel.parseString(markdownString: attributedStringFromResources)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if (!textView.isScrollEnabled) {
textView.isScrollEnabled = true
textView.setContentOffset(.zero, animated: false)
}
}

@IBAction func userDidTapSwitch(_ sender: Any) {
present(actionSheetController, animated: true, completion: nil)
}
}

extension ViewController: UITextViewDelegate {

func textView(_ textView: UITextView, shouldInteractWith URL: URL,
in characterRange: NSRange) -> Bool {
UIApplication.shared.open(URL, options: [:])
Expand All @@ -45,3 +99,4 @@ extension ViewController: UITextViewDelegate {
}

}

2 changes: 1 addition & 1 deletion MarkdownKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MarkdownKit"
s.version = "1.3"
s.version = "1.4"
s.summary = "MarkdownKit is a customizable and extensible Markdown parser for iOS."
s.description = <<-DESC
MarkdownKit is a customizable and extensible Markdown parser for iOS.
Expand Down
Loading

0 comments on commit 64bb17f

Please sign in to comment.