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 indentationStyle to MarkupFormatter.Options #204

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ public struct MarkupFormatter: MarkupWalker {
/// Precede Doxygen commands with an at-sign (`@`).
case at = "@"
}

public enum IndentationStyle {
case spaces // use spaces for indentation
case tabs // Use tab characters for indentation

/// Generates an indentation string based on the current indentation style.
func indentation() -> String {
switch self {
case .spaces:
return String(" ")
case .tabs:
return String("\t")
}
}
}

// MARK: Option Properties

Expand All @@ -253,6 +268,7 @@ public struct MarkupFormatter: MarkupWalker {
var preferredLineLimit: PreferredLineLimit?
var customLinePrefix: String
var doxygenCommandPrefix: DoxygenCommandPrefix
var indentationStyle: IndentationStyle

/**
Create a set of formatting options to use when printing an element.
Expand All @@ -269,6 +285,8 @@ public struct MarkupFormatter: MarkupWalker {
- preferredHeadingStyle: The preferred heading style.
- lineLimit: The preferred maximum line length and method for splitting ``Text`` elements in an attempt to maintain that line length.
- customLinePrefix: An addition prefix to print at the start of each line, useful for adding documentation comment markers.
- indentationStyle: Whether to use spaces or tabs when indenting.

*/
public init(unorderedListMarker: UnorderedListMarker = .dash,
orderedListNumerals: OrderedListNumerals = .allSame(1),
Expand All @@ -281,7 +299,8 @@ public struct MarkupFormatter: MarkupWalker {
preferredHeadingStyle: PreferredHeadingStyle = .atx,
preferredLineLimit: PreferredLineLimit? = nil,
customLinePrefix: String = "",
doxygenCommandPrefix: DoxygenCommandPrefix = .backslash) {
doxygenCommandPrefix: DoxygenCommandPrefix = .backslash,
indentationStyle: IndentationStyle = .spaces) {
self.unorderedListMarker = unorderedListMarker
self.orderedListNumerals = orderedListNumerals
self.useCodeFence = useCodeFence
Expand All @@ -296,6 +315,7 @@ public struct MarkupFormatter: MarkupWalker {
self.thematicBreakLength = max(3, thematicBreakLength)
self.customLinePrefix = customLinePrefix
self.doxygenCommandPrefix = doxygenCommandPrefix
self.indentationStyle = indentationStyle
}

/// The default set of formatting options.
Expand Down Expand Up @@ -431,12 +451,12 @@ public struct MarkupFormatter: MarkupWalker {
prefix += "> "
} else if element is UnorderedList {
if unorderedListCount > 0 {
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
}
unorderedListCount += 1
} else if element is OrderedList {
if orderedListCount > 0 {
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
Comment on lines +454 to +459
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These indentation markers use different numbers of spaces between ordered and unordered lists to align trailing text with the previous line. While it may still parse correctly, this is a regression in behavior.

}
orderedListCount += 1
} else if !(element is ListItem),
Expand All @@ -459,7 +479,7 @@ public struct MarkupFormatter: MarkupWalker {

if parentListItem.parent is UnorderedList {
// Unordered list markers are of fixed length.
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
} else if let numeralPrefix = numeralPrefix(for: parentListItem) {
prefix += String(repeating: " ", count: numeralPrefix.count)
}
Expand Down