-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
735 additions
and
42 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,19 @@ | ||
package msgconv | ||
|
||
import ( | ||
"context" | ||
|
||
"maunium.net/go/mautrix/event" | ||
|
||
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" | ||
"go.mau.fi/mautrix-googlechat/pkg/msgconv/matrixfmt" | ||
) | ||
|
||
func (mc *MessageConverter) ToGChat( | ||
ctx context.Context, | ||
content *event.MessageEventContent, | ||
) (string, []*proto.Annotation) { | ||
parser := &matrixfmt.HTMLParser{} | ||
body, annotations := matrixfmt.Parse(ctx, parser, content) | ||
return body, annotations | ||
} |
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,18 @@ | ||
package gchatfmt | ||
|
||
import "go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" | ||
|
||
func MakeAnnotation(start, length int32, format proto.FormatMetadata_FormatType) *proto.Annotation { | ||
return &proto.Annotation{ | ||
Type: proto.AnnotationType_FORMAT_DATA, | ||
StartIndex: start, | ||
Length: length, | ||
ChipRenderType: proto.Annotation_DO_NOT_RENDER, | ||
Metadata: &proto.Annotation_FormatMetadata{ | ||
FormatMetadata: &proto.FormatMetadata{ | ||
FormatType: format, | ||
}, | ||
}, | ||
} | ||
|
||
} |
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,29 @@ | ||
package matrixfmt | ||
|
||
import ( | ||
"context" | ||
|
||
"maunium.net/go/mautrix/event" | ||
|
||
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" | ||
) | ||
|
||
func Parse(ctx context.Context, parser *HTMLParser, content *event.MessageEventContent) (string, []*proto.Annotation) { | ||
if content.Format != event.FormatHTML { | ||
return content.Body, nil | ||
} | ||
parseCtx := NewContext(ctx) | ||
parseCtx.AllowedMentions = content.Mentions | ||
parsed := parser.Parse(content.FormattedBody, parseCtx) | ||
if parsed == nil { | ||
return "", nil | ||
} | ||
var bodyRanges []*proto.Annotation | ||
if len(parsed.Entities) > 0 { | ||
bodyRanges = make([]*proto.Annotation, len(parsed.Entities)) | ||
for i, ent := range parsed.Entities { | ||
bodyRanges[i] = ent.Proto() | ||
} | ||
} | ||
return parsed.String.String(), bodyRanges | ||
} |
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,46 @@ | ||
package matrixfmt_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"maunium.net/go/mautrix/event" | ||
|
||
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" | ||
"go.mau.fi/mautrix-googlechat/pkg/msgconv/gchatfmt" | ||
"go.mau.fi/mautrix-googlechat/pkg/msgconv/matrixfmt" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
out string | ||
ent []*proto.Annotation | ||
}{ | ||
{name: "Plain", in: "Hello, World!", out: "Hello, World!"}, | ||
{name: "Bold", in: "a <strong>b</strong> c", out: "a b c", | ||
ent: []*proto.Annotation{ | ||
gchatfmt.MakeAnnotation(2, 1, proto.FormatMetadata_BOLD), | ||
}, | ||
}, | ||
} | ||
|
||
parser := &matrixfmt.HTMLParser{} | ||
matrixfmt.DebugLog = func(format string, args ...any) { | ||
fmt.Printf(format, args...) | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
parsed, entities := matrixfmt.Parse(context.TODO(), parser, &event.MessageEventContent{ | ||
Format: event.FormatHTML, | ||
FormattedBody: test.in, | ||
}) | ||
assert.Equal(t, test.out, parsed) | ||
assert.Equal(t, test.ent, entities) | ||
}) | ||
} | ||
} |
Oops, something went wrong.