mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 11:40:28 +08:00
Parse runs
This commit is contained in:
@@ -31,6 +31,9 @@ func TestStructure(t *testing.T) {
|
|||||||
if child.Link == nil && child.Properties == nil && child.Run == nil {
|
if child.Link == nil && child.Properties == nil && child.Run == nil {
|
||||||
t.Errorf("There are children with all fields nil")
|
t.Errorf("There are children with all fields nil")
|
||||||
}
|
}
|
||||||
|
if child.Run != nil && child.Run.Text == nil {
|
||||||
|
t.Errorf("We have a run with no text")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
structrun.go
38
structrun.go
@@ -1,6 +1,9 @@
|
|||||||
package docxlib
|
package docxlib
|
||||||
|
|
||||||
import "encoding/xml"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HYPERLINK_STYLE = "a1"
|
HYPERLINK_STYLE = "a1"
|
||||||
@@ -62,3 +65,36 @@ type Size struct {
|
|||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main sz"`
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main sz"`
|
||||||
Val int `xml:"w:val,attr"`
|
Val int `xml:"w:val,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
|
var elem Run
|
||||||
|
for {
|
||||||
|
t, err := d.Token()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
switch tt := t.(type) {
|
||||||
|
case xml.CharData:
|
||||||
|
var value string
|
||||||
|
d.DecodeElement(&value, &start)
|
||||||
|
elem.Text = &Text{Text: value}
|
||||||
|
case xml.StartElement:
|
||||||
|
if tt.Name.Local == "rPr" {
|
||||||
|
var value RunProperties
|
||||||
|
d.DecodeElement(&value, &start)
|
||||||
|
elem.RunProperties = &value
|
||||||
|
} else if tt.Name.Local == "instrText" {
|
||||||
|
var value string
|
||||||
|
d.DecodeElement(&value, &start)
|
||||||
|
elem.InstrText = value
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*r = elem
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user