Files
GitHawk/Classes/Issues/Comments/Markdown/CodeBlockModel.swift
Ryan Nystrom c23945ac7d Enable SwiftLint and fix issues (#2292)
* update swiftlint

* build with lint enabled

* fix almost all warnings

* remove wholemodule
2018-10-16 21:54:55 -04:00

52 lines
1.6 KiB
Swift

//
// CodeBlockModel.swift
// Freetime
//
// Created by Ryan Nystrom on 4/7/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import Foundation
import StyledTextKit
func CodeBlockElement(
text: String,
language: String?,
contentSizeCategory: UIContentSizeCategory
) -> IssueCommentCodeBlockModel {
let trimmedText = text.trimmingCharacters(in: .whitespacesAndNewlines)
let attributedString: NSAttributedString
let fixedLanguage = language?.isEmpty == true ? nil : language
if let language = fixedLanguage,
let highlighted = GithubHighlighting.highlight(trimmedText, as: language) {
attributedString = highlighted
} else {
attributedString = NSAttributedString(
string: trimmedText,
attributes: [
.foregroundColor: Styles.Colors.Gray.dark.color,
.font: Styles.Text.code.font(contentSizeCategory: contentSizeCategory)
]
)
}
var inset = IssueCommentCodeBlockCell.textViewInset
inset.left += IssueCommentCodeBlockCell.scrollViewInset.left
inset.right += IssueCommentCodeBlockCell.scrollViewInset.right
let backgroundColor = Styles.Colors.Gray.lighter.color
let builder = StyledTextBuilder(attributedText: attributedString)
.add(attributes: [.backgroundColor: backgroundColor])
let string = StyledTextRenderer(
string: builder.build(),
contentSizeCategory: contentSizeCategory,
inset: inset,
backgroundColor: backgroundColor
).warm(width: 0)
return IssueCommentCodeBlockModel(
code: string,
language: fixedLanguage
)
}