Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTML Encoder / Decoder #6

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"version": "2.6.3"
}
},
{
"package": "HTMLEntities",
"repositoryURL": "https://github.com/kkk669/swift-html-entities",
"state": {
"branch": null,
"revision": "39083f7a1826b1b01e3a270795ed409ac4ccea18",
"version": "4.0.1"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
Expand Down
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/kkk669/swift-log-playground", "0.1.0"..<"0.2.0"),
.package(url: "https://github.com/luin/SwiftJSONFormatter", "1.0.0"..<"1.1.0"),
.package(url: "https://github.com/auth0/JWTDecode.swift", "2.6.3"..<"2.7.0")
.package(url: "https://github.com/auth0/JWTDecode.swift", "2.6.3"..<"2.7.0"),
.package(url: "https://github.com/kkk669/swift-html-entities", "4.0.1"..<"4.1.0")
],
targets: [
.executableTarget(
name: "DevToysApp",
dependencies: [
.product(name: "LoggingPlayground", package: "swift-log-playground"),
.product(name: "SwiftJSONFormatter", package: "SwiftJSONFormatter"),
.product(name: "JWTDecode", package: "JWTDecode.swift")
.product(name: "JWTDecode", package: "JWTDecode.swift"),
.product(name: "HTMLEntities", package: "swift-html-entities")
],
swiftSettings: [
.unsafeFlags(["-Xfrontend", "-warn-long-function-bodies=100"], .when(configuration: .debug)),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This app is a SwiftUI reimplementation of [DevToys](https://devtoys.app), a Swis
- [ ] JSON <> YAML
- [x] Number Base
- Encoders / Decoders
- [ ] HTML
- [x] HTML
- [x] URL
- [x] Base64
- [ ] GZip
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Combine
import HTMLEntities

final class HTMLEncoderDecoderViewModel {
@Published var encodeMode = true
@Published var input = ""
@Published var output = ""

init() {
self.$input.combineLatest(self.$encodeMode)
.dropFirst()
.map { input, encodeMode in
encodeMode
? Self.encode(input)
: Self.decode(input)
}
.assign(to: &self.$output)
}

private static func encode(_ input: String) -> String {
input.htmlEscape(useNamedReferences: true)
}

private static func decode(_ input: String) -> String {
input.htmlUnescape()
}
}

extension HTMLEncoderDecoderViewModel: ObservableObject {}
20 changes: 20 additions & 0 deletions Sources/DevToysApp/Views/AllToolsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ extension AllToolsView: View {
}
}
}
if !self.isSearching
|| self.searchQuery.isEmpty
|| "HTML Encoder / Decoder".lowercased()
.contains(self.searchQuery.lowercased())
{
NavigationLink {
HTMLEncoderDecoderView()
} label: {
Label {
Text("HTML")
Text(
"Encode or decode all the applicable characters to their corresponding HTML entities"
)
.font(.caption)
.foregroundStyle(.secondary)
} icon: {
Image(systemName: "chevron.left.slash.chevron.right")
}
}
}
if !self.isSearching
|| self.searchQuery.isEmpty
|| "JSON Formatter".lowercased()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SwiftUI

struct HTMLEncoderDecoderView {
@StateObject private var viewModel = HTMLEncoderDecoderViewModel()

init() {
Task { @MainActor in
UITextView.appearance().backgroundColor = .clear
}
}
}

extension HTMLEncoderDecoderView: View {
var body: some View {
ToyPage {
ToySection("Configuration") {
ConfigurationRow(systemImage: "arrow.left.arrow.right") {
Text("Conversion")
Text("Select which conversion mode you want to use")
.font(.caption)
.foregroundStyle(.secondary)
} content: {
Picker("", selection: self.$viewModel.encodeMode) {
Text("Encode").tag(true)
Text("Decode").tag(false)
}
}
}

ToySection("Input") {
PasteButton(text: self.$viewModel.input)
OpenFileButton(text: self.$viewModel.input)
ClearButton(text: self.$viewModel.input)
} content: {
TextEditor(text: self.$viewModel.input)
.disableAutocorrection(true)
.textInputAutocapitalization(.never)
.font(.body.monospaced())
.background(.regularMaterial)
.cornerRadius(8)
.frame(idealHeight: 200)
}

ToySection("Output") {
CopyButton(text: self.viewModel.output)
} content: {
TextEditor(text: .constant(self.viewModel.output))
.disableAutocorrection(true)
.textInputAutocapitalization(.never)
.font(.body.monospaced())
.background(.regularMaterial)
.cornerRadius(8)
.frame(idealHeight: 200)
}
}
.navigationTitle("HTML Encoder / Decoder")
}
}

struct HTMLEncoderDecoderView_Previews: PreviewProvider {
static var previews: some View {
HTMLEncoderDecoderView()
}
}
18 changes: 18 additions & 0 deletions Sources/DevToysApp/Views/Sidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ extension Sidebar: View {
Text("Converters").font(.title3.bold())
}
Section {
NavigationLink {
HTMLEncoderDecoderView()
} label: {
Label(
"HTML",
systemImage: "chevron.left.slash.chevron.right"
)
}
NavigationLink {
URLEncoderDecoderView()
} label: {
Expand Down Expand Up @@ -113,6 +121,16 @@ extension Sidebar: View {
Label("Number Base Converter", systemImage: "number.square")
}
}
if self.isMatch("HTML Encoder / Decoder") {
NavigationLink {
HTMLEncoderDecoderView()
} label: {
Label(
"HTML Encoder / Decoder",
systemImage: "chevron.left.slash.chevron.right"
)
}
}
if self.isMatch("URL Encoder / Decoder") {
NavigationLink {
URLEncoderDecoderView()
Expand Down