-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better description generation for events and commands
- Loading branch information
Showing
5 changed files
with
136 additions
and
9 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
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,122 @@ | ||
package spec | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/project-chip/alchemy/asciidoc" | ||
"github.com/project-chip/alchemy/matter" | ||
) | ||
|
||
var endOfSentencePattern = regexp.MustCompile(`(?m)(\.( |$)|\n\n)`) | ||
|
||
func getDescription(doc *Doc, els asciidoc.Set) string { | ||
var sb strings.Builder | ||
readDescription(doc, els, &sb) | ||
description := sb.String() | ||
endOfSentence := endOfSentencePattern.FindStringIndex(description) | ||
if endOfSentence != nil { | ||
endOfSentenceIndex := endOfSentence[0] | ||
if description[endOfSentenceIndex] == '.' { | ||
endOfSentenceIndex++ | ||
} | ||
description = description[:endOfSentenceIndex] | ||
} | ||
return description | ||
} | ||
|
||
func readDescription(doc *Doc, els asciidoc.Set, value *strings.Builder) (err error) { | ||
var foundNonBlock bool | ||
for _, el := range els { | ||
var e asciidoc.Element | ||
switch el := el.(type) { | ||
case *Element: | ||
e = el.Base | ||
case asciidoc.Element: | ||
e = el | ||
default: | ||
return | ||
} | ||
|
||
switch e.Type() { | ||
case asciidoc.ElementTypeBlock, asciidoc.ElementTypeDocument: | ||
if foundNonBlock { | ||
return | ||
} | ||
continue | ||
case asciidoc.ElementTypeAttribute, asciidoc.ElementTypeAttributes: | ||
continue | ||
} | ||
foundNonBlock = true | ||
switch el := e.(type) { | ||
case *asciidoc.String: | ||
value.WriteString(el.Value) | ||
case asciidoc.FormattedTextElement: | ||
err = readDescription(doc, el.Elements(), value) | ||
case *asciidoc.CrossReference: | ||
if len(el.Set) > 0 { | ||
var label strings.Builder | ||
readDescription(doc, el.Set, &label) | ||
value.WriteString(strings.TrimSpace(label.String())) | ||
} else { | ||
var val string | ||
anchor := doc.FindAnchor(el.ID) | ||
if anchor != nil { | ||
val = matter.StripTypeSuffixes(ReferenceName(anchor.Element)) | ||
} else { | ||
val = strings.TrimPrefix(el.ID, "_") | ||
val = strings.TrimPrefix(val, "ref_") // Trim, and hope someone else has it defined | ||
} | ||
value.WriteString(val) | ||
} | ||
case *asciidoc.Link: | ||
value.WriteString(el.URL.Scheme) | ||
readDescription(doc, el.URL.Path, value) | ||
case *asciidoc.LinkMacro: | ||
value.WriteString(el.URL.Scheme) | ||
readDescription(doc, el.URL.Path, value) | ||
case *asciidoc.Superscript: | ||
// In the special case of superscript elements, we do checks to make sure it's not an asterisk or a footnote, which should be ignored | ||
var quotedText strings.Builder | ||
err = readDescription(doc, el.Elements(), "edText) | ||
if err != nil { | ||
return | ||
} | ||
qt := quotedText.String() | ||
if qt == "*" { // | ||
continue | ||
} | ||
_, parseErr := strconv.Atoi(qt) | ||
if parseErr == nil { | ||
// This is probably a footnote | ||
// The similar buildConstraintValue method does not do this, as there are exponential values in contraints | ||
continue | ||
} | ||
value.WriteString(qt) | ||
case *asciidoc.SpecialCharacter: | ||
value.WriteString(el.Character) | ||
case *asciidoc.InlinePassthrough: | ||
value.WriteString("+") | ||
err = readDescription(doc, el.Elements(), value) | ||
case *asciidoc.InlineDoublePassthrough: | ||
value.WriteString("++") | ||
err = readDescription(doc, el.Elements(), value) | ||
case *asciidoc.ThematicBreak: | ||
case asciidoc.EmptyLine: | ||
case *asciidoc.NewLine: | ||
value.WriteString(" ") | ||
case asciidoc.HasElements: | ||
err = readDescription(doc, el.Elements(), value) | ||
case *asciidoc.LineBreak: | ||
value.WriteString(" ") | ||
default: | ||
return fmt.Errorf("unexpected type in description: %T", el) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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