-
Notifications
You must be signed in to change notification settings - Fork 143
Add support for @Small directive #380
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2022 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Foundation | ||
import Markdown | ||
|
||
/// A directive for specifying small print text like legal, license, or copyright text that | ||
/// should be rendered in a smaller font size. | ||
/// | ||
/// The `@Small` directive is based on HTML's small tag (`<small>`). It supports any inline markup | ||
/// formatting like bold and italics but does not support more structured markup like ``Row`` | ||
/// and ``Row/Column``. | ||
/// | ||
/// ```md | ||
/// You can create a sloth using the ``init(name:color:power:)`` | ||
/// initializer, or create randomly generated sloth using a | ||
/// ``SlothGenerator``: | ||
/// | ||
/// let slothGenerator = MySlothGenerator(seed: randomSeed()) | ||
/// let habitat = Habitat(isHumid: false, isWarm: true) | ||
/// | ||
/// // ... | ||
/// | ||
/// @Small { | ||
/// _Licensed under Apache License v2.0 with Runtime Library Exception._ | ||
/// } | ||
/// ``` | ||
public final class Small: Semantic, AutomaticDirectiveConvertible, MarkupContaining { | ||
public let originalMarkup: BlockDirective | ||
|
||
/// The inline markup that should be rendered in a small font. | ||
@ChildMarkup(numberOfParagraphs: .oneOrMore) | ||
public private(set) var content: MarkupContainer | ||
|
||
static var keyPaths: [String : AnyKeyPath] = [ | ||
"content" : \Small._content, | ||
] | ||
|
||
override var children: [Semantic] { | ||
return [content] | ||
} | ||
|
||
var childMarkup: [Markup] { | ||
return content.elements | ||
} | ||
|
||
@available(*, deprecated, | ||
message: "Do not call directly. Required for 'AutomaticDirectiveConvertible'." | ||
) | ||
init(originalMarkup: BlockDirective) { | ||
self.originalMarkup = originalMarkup | ||
} | ||
} | ||
|
||
extension Small: RenderableDirectiveConvertible { | ||
func render(with contentCompiler: inout RenderContentCompiler) -> [RenderContent] { | ||
// Render the content normally | ||
let renderBlockContent = content.elements.flatMap { markupElement in | ||
return contentCompiler.visit(markupElement) as! [RenderBlockContent] | ||
} | ||
|
||
// Transform every paragraph in the render block content to a small paragraph | ||
let transformedRenderBlockContent = renderBlockContent.map { block -> RenderBlockContent in | ||
guard case let .paragraph(paragraph) = block else { | ||
return block | ||
} | ||
|
||
return .small(RenderBlockContent.Small(inlineContent: paragraph.inlineContent)) | ||
} | ||
|
||
return transformedRenderBlockContent | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1058,4 +1058,37 @@ class RenderNodeTranslatorTests: XCTestCase { | |
XCTAssertEqual(row.columns.last?.size, 5) | ||
XCTAssertEqual(row.columns.last?.content.count, 3) | ||
} | ||
|
||
func testSmall() throws { | ||
let (bundle, context) = try testBundleAndContext(named: "BookLikeContent") | ||
let reference = ResolvedTopicReference( | ||
bundleIdentifier: bundle.identifier, | ||
path: "/documentation/BestBook/MyArticle", | ||
sourceLanguage: .swift | ||
) | ||
let article = try XCTUnwrap(context.entity(with: reference).semantic as? Article) | ||
var translator = RenderNodeTranslator( | ||
context: context, | ||
bundle: bundle, | ||
identifier: reference, | ||
source: nil | ||
) | ||
let renderNode = try XCTUnwrap(translator.visitArticle(article) as? RenderNode) | ||
|
||
let discussion = try XCTUnwrap( | ||
renderNode.primaryContentSections.first( | ||
where: { $0.kind == .content } | ||
) as? ContentRenderSection | ||
) | ||
|
||
guard case let .small(small) = discussion.content.last else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we not have a separate test from row and column one for this? I find that having smaller test cases helps pin out failures faster. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good catch- I meant to create a new one here. Should be fixed now. Thanks! |
||
XCTFail("Expected to find small as last child.") | ||
return | ||
} | ||
|
||
XCTAssertEqual( | ||
small.inlineContent, | ||
[.text("Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved.")] | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,9 @@ class RowTests: XCTestCase { | |
["1: warning – org.swift.docc.HasAtLeastOne<Row, Column>"] | ||
) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this not be in a separate patch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split into a separate commit here: e4a6fc4. |
||
.row(.init(numberOfColumns: 0, columns: [])) | ||
) | ||
} | ||
|
@@ -65,8 +66,9 @@ class RowTests: XCTestCase { | |
] | ||
) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row( | ||
numberOfColumns: 6, | ||
columns: [ | ||
|
@@ -129,8 +131,9 @@ class RowTests: XCTestCase { | |
] | ||
) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row( | ||
numberOfColumns: 0, | ||
columns: [] | ||
|
@@ -159,8 +162,9 @@ class RowTests: XCTestCase { | |
] | ||
) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row( | ||
numberOfColumns: 1, | ||
columns: [ | ||
|
@@ -187,8 +191,9 @@ class RowTests: XCTestCase { | |
] | ||
) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row(numberOfColumns: 0, columns: [])) | ||
) | ||
} | ||
|
@@ -212,8 +217,9 @@ class RowTests: XCTestCase { | |
XCTAssertNotNil(row) | ||
XCTAssertEqual(problems, []) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row( | ||
numberOfColumns: 5, | ||
columns: [ | ||
|
@@ -252,8 +258,9 @@ class RowTests: XCTestCase { | |
XCTAssertNotNil(row) | ||
XCTAssertEqual(problems, []) | ||
|
||
XCTAssertEqual(renderBlockContent.count, 1) | ||
XCTAssertEqual( | ||
renderBlockContent, | ||
renderBlockContent.first, | ||
.row(RenderBlockContent.Row( | ||
numberOfColumns: 1, | ||
columns: [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mega nit, this probably belongs in another patch.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah agreed- I just missed it in #378 so I thought it was worth cleaning up here. I can split it into a separate commit though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split out here: e56bd43