-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ACTIONS_REQUIRED blocks from releases and PRs. (#40)
- Loading branch information
Showing
7 changed files
with
473 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package markdown | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var NoSuchBlockError = fmt.Errorf("no such block") | ||
|
||
func isHeading(l string) bool { | ||
return strings.HasPrefix(l, "#") | ||
} | ||
|
||
func headingLevel(l string) int { | ||
level := 0 | ||
for _, char := range l { | ||
if char != '#' { | ||
break | ||
} | ||
level++ | ||
} | ||
return level | ||
} | ||
|
||
func ExtractAnnotatedBlock(annotation string, s string) (string, error) { | ||
parts := strings.SplitN(s, "```"+annotation, 2) | ||
if len(parts) != 2 { | ||
return "", NoSuchBlockError | ||
} | ||
|
||
parts = strings.SplitN(parts[1], "```", 2) | ||
if len(parts) != 2 { | ||
return "", NoSuchBlockError | ||
} | ||
|
||
return strings.TrimSpace(parts[0]), nil | ||
} | ||
|
||
func ToListItem(lines string) []string { | ||
var result []string | ||
|
||
for i, line := range SplitLines(lines) { | ||
if i == 0 { | ||
result = append(result, "* "+line) | ||
continue | ||
} | ||
|
||
result = append(result, " "+line) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func SplitLines(s string) []string { | ||
return strings.Split(strings.Replace(s, `\r\n`, "\n", -1), "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package markdown | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type MarkdownSection struct { | ||
Level int | ||
Heading string | ||
ContentLines []string | ||
SubSections []*MarkdownSection | ||
} | ||
|
||
func (m *MarkdownSection) allSections() []*MarkdownSection { | ||
var result []*MarkdownSection | ||
|
||
result = append(result, m) | ||
|
||
for _, s := range m.SubSections { | ||
result = append(result, s.allSections()...) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (m *MarkdownSection) FindSectionByHeading(level int, headline string) *MarkdownSection { | ||
for _, s := range m.allSections() { | ||
if s.Level == level { | ||
if headline == s.Heading { | ||
return s | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MarkdownSection) AppendContent(contentLines []string) { | ||
m.ContentLines = append(m.ContentLines, contentLines...) | ||
} | ||
|
||
func (m *MarkdownSection) PrependContent(contentLines []string) { | ||
m.ContentLines = append(contentLines, m.ContentLines...) | ||
} | ||
|
||
func (m *MarkdownSection) AppendChild(child *MarkdownSection) { | ||
m.SubSections = append(m.SubSections, child) | ||
} | ||
|
||
func (m *MarkdownSection) PrependChild(child *MarkdownSection) { | ||
m.SubSections = append([]*MarkdownSection{child}, m.SubSections...) | ||
} | ||
|
||
func (m *MarkdownSection) String() string { | ||
var result string | ||
|
||
if m.Level > 0 { | ||
for i := 0; i < m.Level; i++ { | ||
result += "#" | ||
} | ||
result += " " + m.Heading + "\n" | ||
} | ||
|
||
for _, l := range m.ContentLines { | ||
result += fmt.Sprintf("%s\n", l) | ||
} | ||
|
||
result = strings.Trim(result, "\n") | ||
|
||
for _, sub := range m.SubSections { | ||
result += "\n" + sub.String() | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.