Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Aug 11, 2021
2 parents 48b23eb + 4699dfe commit 9b78fd5
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 28 deletions.
62 changes: 41 additions & 21 deletions Sources/DocCArchive/Schema_0_1/Content/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ extension DocCArchive.DocCSchema_0_1 {
public enum Style: String, Codable {
case note
}

case heading (text: String, anchor: String, level: Int)
case aside (style: Style, content: [ Content ])
case paragraph (inlineContent: [ InlineContent ])
case codeListing(CodeListing)
case step (Step)

public struct Item: Equatable, Codable {
public var content : [ Content ]
}

case heading (text: String, anchor: String, level: Int)
case aside (style: Style, content: [ Content ])
case paragraph (inlineContent: [ InlineContent ])
case codeListing (CodeListing)
case step (Step)
case orderedList ([ Item ])
case unorderedList([ Item ])

public var description: String {
switch self {
case .heading (let text, let anchor, let level):
Expand All @@ -32,16 +38,18 @@ extension DocCArchive.DocCSchema_0_1 {
return ms + ">"
case .aside (let style, let content):
return "<aside[\(style)]: \(content)>"
case .paragraph (let inlineContent) : return "<p>\(inlineContent)</p>"
case .codeListing(let code) : return code.description
case .step (let step) : return step.description
case .orderedList (let items) : return "<ol>\(items)</ol>"
case .unorderedList(let items) : return "<ul>\(items)</ul>"
case .paragraph (let icontent) : return "<p>\(icontent)</p>"
case .codeListing (let code) : return code.description
case .step (let step) : return step.description
}
}

// - MARK: Codable

private enum CodingKeys: String, CodingKey {
case content, anchor, level, type, text, style, inlineContent
case content, anchor, level, type, text, style, inlineContent, items
}

public init(from decoder: Decoder) throws {
Expand All @@ -58,6 +66,12 @@ extension DocCArchive.DocCSchema_0_1 {
let style = try container.decode(Style.self , forKey: .style)
let content = try container.decode([Content].self , forKey: .content)
self = .aside(style: style, content: content)
case "orderedList":
let content = try container.decode([ Item ].self, forKey: .items)
self = .orderedList(content)
case "unorderedList":
let content = try container.decode([ Item ].self, forKey: .items)
self = .unorderedList(content)
case "paragraph":
let content = try container.decode([ InlineContent ].self ,
forKey: .inlineContent)
Expand All @@ -78,22 +92,28 @@ extension DocCArchive.DocCSchema_0_1 {

switch self {
case .heading(let text, let anchor, let level):
try container.encode("heading" , forKey: .type)
try container.encode(text , forKey: .text)
try container.encode(anchor , forKey: .anchor)
try container.encode(level , forKey: .level)
try container.encode("heading" , forKey: .type)
try container.encode(text , forKey: .text)
try container.encode(anchor , forKey: .anchor)
try container.encode(level , forKey: .level)
case .aside(let style, let content):
try container.encode("aside" , forKey: .type)
try container.encode(style , forKey: .style)
try container.encode(content , forKey: .content)
try container.encode("aside" , forKey: .type)
try container.encode(style , forKey: .style)
try container.encode(content , forKey: .content)
case .unorderedList(let items):
try container.encode("unorderedList" , forKey: .type)
try container.encode(items , forKey: .items)
case .orderedList(let items):
try container.encode("orderedList" , forKey: .type)
try container.encode(items , forKey: .items)
case .paragraph(let content):
try container.encode("paragraph" , forKey: .type)
try container.encode(content , forKey: .inlineContent)
try container.encode("paragraph" , forKey: .type)
try container.encode(content , forKey: .inlineContent)
case .codeListing(let content):
try container.encode("codeListing" , forKey: .type)
try container.encode("codeListing" , forKey: .type)
try content.encode(to: encoder)
case .step(let content):
try container.encode("step" , forKey: .type)
try container.encode("step" , forKey: .type)
try content.encode(to: encoder)
}
}
Expand Down
22 changes: 18 additions & 4 deletions Sources/DocCArchive/Schema_0_1/InlineContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ extension DocCArchive.DocCSchema_0_1 {
// Fragments have a 'kind', InlineContent has a 'type'

case text (String)
case reference(identifier: Identifier, isActive: Bool)
case reference(identifier: Identifier, isActive: Bool,
overridingTitle : String?,
overridingTitleInlineContent : [ InlineContent ]?)
case image (identifier: String)
case emphasis ([ InlineContent ])
case codeVoice(code: String)

public var description: String {
switch self {
case .text (let s) : return "\(s)"
case .reference(let id, let isActive):
case .reference(let id, let isActive, _, _):
return "\(id)\(isActive ? "" : "-inactive")"
case .image (let id) : return "<img \(id)>"
case .emphasis (let content) : return "*\(content)*"
Expand All @@ -33,6 +35,7 @@ extension DocCArchive.DocCSchema_0_1 {

private enum CodingKeys: String, CodingKey {
case type, text, identifier, isActive, inlineContent, code
case overridingTitle, overridingTitleInlineContent
}

public init(from decoder: Decoder) throws {
Expand All @@ -57,7 +60,14 @@ extension DocCArchive.DocCSchema_0_1 {
try container.decode(Identifier.self, forKey: .identifier),
isActive:
try container.decodeIfPresent(Bool.self, forKey: .isActive)
?? true
?? true,
overridingTitle:
try container
.decodeIfPresent(String.self, forKey: .overridingTitle),
overridingTitleInlineContent:
try container
.decodeIfPresent([ InlineContent ].self,
forKey: .overridingTitleInlineContent)
)
default:
throw DocCArchiveLoadingError.unsupportedInlineContentType(kind)
Expand All @@ -80,10 +90,14 @@ extension DocCArchive.DocCSchema_0_1 {
case .emphasis(let content):
try container.encode("emphasis" , forKey: .type)
try container.encode(content , forKey: .inlineContent)
case .reference(let identifier, let isActive):
case .reference(let identifier, let isActive, let ot, let otc):
try container.encode("reference" , forKey: .type)
try container.encode(identifier , forKey: .identifier)
try container.encode(isActive , forKey: .isActive)
if let v = ot { try container.encode(v , forKey: .overridingTitle) }
if let v = otc {
try container.encode(v, forKey: .overridingTitleInlineContent)
}
}
}
}
Expand Down
53 changes: 50 additions & 3 deletions Tests/DocCArchiveTests/DocumentDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,58 @@ import XCTest
final class DocumentDecodingTests: XCTestCase {

static var allTests = [
( "testSimpleTutorial" , testSimpleTutorial ),
( "testIssue7Fail" , testIssue7Fail ),
( "testAllDataJSONInSlothCreator" , testAllDataJSONInSlothCreator )
( "testSimpleTutorial" , testSimpleTutorial ),
( "testIssue7Fail" , testIssue7Fail ),
( "testIssue9FailAttributeFragment" , testIssue9FailAttributeFragment ),
( "testIssue10FailTypeMethodRoleHeading",
testIssue10FailTypeMethodRoleHeading ),
( "testIssue11FailUnorderedList" , testIssue11FailUnorderedList ),
( "testAllDataJSONInSlothCreator" , testAllDataJSONInSlothCreator )
]

func testIssue11FailUnorderedList() throws {
let url = Fixtures.baseURL.appendingPathComponent("Issue11Fail.json")
let data = try Data(contentsOf: url)

let document : DocCArchive.Document

print("Decoding:", url.path)
do {
document = try JSONDecoder().decode(DocCArchive.Document.self, from: data)

guard let section = document.primaryContentSections?.first else {
XCTAssert(false, "did not find primary content section"); return
}
guard case .content(let contents) = section.kind else {
XCTAssert(false, "did not find content section"); return
}
guard case .unorderedList(let list) = contents.dropFirst().first else {
XCTAssert(false, "did not find list"); return
}
XCTAssertEqual(list.count, 2)
guard case .paragraph(let inlineContent) = list.last?.content.first else {
XCTAssert(false, "did not find paragraph"); return
}
XCTAssertEqual(inlineContent.count, 2)
guard case .reference(let id, let isActive, let ot, let otc) =
inlineContent.last else
{
XCTAssert(false, "did not find reference"); return
}
XCTAssertTrue(isActive)
XCTAssertEqual(id, DocCArchive.DocCSchema_0_1.Identifier(
url: URL(string: "https://github.com/3Qax")!))
XCTAssertEqual(ot , "@3Qax")
XCTAssertEqual(otc?.count , 1)
}
catch {
print("ERROR:", error)
XCTAssert(false, "failed to decode: \(error)")
return
}
XCTAssertEqual(document.schemaVersion, .init(major: 0, minor: 1, patch: 0))
}

func testIssue10FailTypeMethodRoleHeading() throws {
let url = Fixtures.baseURL.appendingPathComponent("Issue10Fail.json")
let data = try Data(contentsOf: url)
Expand Down
59 changes: 59 additions & 0 deletions Tests/DocCArchiveTests/Fixtures/Issue11Fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"primaryContentSections": [
{
"kind": "content",
"content": [
{ "anchor": "Added", "level": 3, "type": "heading", "text": "Added" },
{
"type": "unorderedList",
"items": [
{
"content": [
{
"type": "paragraph",
"inlineContent": [
{ "type": "text", "text": "Counter component " },
{ "type": "reference", "isActive": true,
"identifier": "https://github.com/3Qax" }
]
}
]
},
{
"content": [
{
"type": "paragraph",
"inlineContent": [
{ "type": "text", "text": "Changelog.md" },
{ "type": "reference", "isActive": true,
"identifier": "https://github.com/3Qax",
"overridingTitle": "@3Qax",
"overridingTitleInlineContent": [
{ "type": "text", "text": "@3Qax" }
]
}
]
}
]
}
]
}
]
}
],
"schemaVersion":{"major":0,"minor":1,"patch":0},
"identifier":{
"url":"doc://ARI/documentation/Fail9",
"interfaceLanguage":"swift"
},
"kind":"symbol",
"metadata": {
"title": "And then she kissed her",
"categoryPathComponent": "Songs",
"role": "project",
"category": "Songs"
},
"hierarchy":{ "paths":[] },
"sections":[],
"references":{}
}

0 comments on commit 9b78fd5

Please sign in to comment.