Example #1
0
File: parser.go Project: husio/apps
func (ap *Parser) parseAtomText(p *xpp.XMLPullParser) (string, error) {

	var text struct {
		Type     string `xml:"type,attr"`
		Mode     string `xml:"mode,attr"`
		Body     string `xml:",chardata"`
		InnerXML string `xml:",innerxml"`
	}

	err := p.DecodeElement(&text)
	if err != nil {
		return "", err
	}

	result := text.InnerXML
	result = strings.TrimSpace(result)
	result = strings.TrimPrefix(result, "<![CDATA[")
	result = strings.TrimSuffix(result, "]]>")
	result = strings.TrimSpace(result)

	lowerType := strings.ToLower(text.Type)
	lowerMode := strings.ToLower(text.Mode)

	if lowerType == "html" {
		result = shared.DecodeEntities(result)
	}

	if lowerType == "text" ||
		strings.HasPrefix(lowerType, "text/") ||
		(lowerType == "" && lowerMode == "") {
		result = shared.DecodeEntities(result)
	} else if strings.Contains(lowerType, "xhtml") ||
		lowerType == "html" {
		r := strings.NewReader(result)
		doc, err := goquery.NewDocumentFromReader(r)
		if err == nil {
			outerElement := doc.Find("div").First()
			if outerElement.Is("div") {
				html, err := outerElement.Unwrap().Html()
				if err == nil {
					result = html
				}
			}
		}
	} else {
		decodedStr, err := base64.StdEncoding.DecodeString(result)
		if err == nil {
			result = string(decodedStr)
		}
	}

	return result, nil
}
Example #2
0
func (ap *Parser) parseAtomText(p *xpp.XMLPullParser) (string, error) {

	var text struct {
		Type     string `xml:"type,attr"`
		Mode     string `xml:"mode,attr"`
		InnerXML string `xml:",innerxml"`
	}

	err := p.DecodeElement(&text)
	if err != nil {
		return "", err
	}

	result := text.InnerXML
	result = strings.TrimSpace(result)

	if strings.HasPrefix(result, "<![CDATA[") &&
		strings.HasSuffix(result, "]]>") {
		result = strings.TrimPrefix(result, "<![CDATA[")
		result = strings.TrimSuffix(result, "]]>")
		return result, nil
	}

	lowerType := strings.ToLower(text.Type)
	lowerMode := strings.ToLower(text.Mode)

	if lowerType == "text" ||
		strings.HasPrefix(lowerType, "text/") ||
		(lowerType == "" && lowerMode == "") {
		result, err = shared.DecodeEntities(result)
	} else if strings.Contains(lowerType, "xhtml") {
		result = ap.stripWrappingDiv(result)
	} else if lowerType == "html" {
		result = ap.stripWrappingDiv(result)
		result, err = shared.DecodeEntities(result)
	} else {
		decodedStr, err := base64.StdEncoding.DecodeString(result)
		if err == nil {
			result = string(decodedStr)
		}
	}

	return result, err
}