예제 #1
0
func getAllFrontPages(DocRoot string) (PageList []string, ErrorResult error) {
	PageList = make([]string, 0)

	PagesDir := filepath.Join(DocRoot, "pages")

	files, err := ioutil.ReadDir(PagesDir)
	if err != nil {
		return nil, err
	}

	for _, f := range files {
		if f.IsDir() {
			DocName := malkovich.FolderNameToDocName(f.Name())
			pageDirectory := getDocDirectory(DocRoot, DocName)
			PageDB, err := openPageDatabase(DocName, pageDirectory)
			if err == nil {
				exists, value := readPageAttribute(PageDB, `IsOnFrontPage`)
				valueAsBool, _ := strconv.ParseBool(value)
				if (exists == true) && (valueAsBool == true) {
					PageList = append(PageList, f.Name())
				}
			}
			closePageDatabase(PageDB)
		}
	}
	return PageList, nil
}
예제 #2
0
func getPageInfoAsJsonData(DocRoot string, PageList []string) (Data string, ErrorResult error) {
	var pageSummaryData PageSummaryData
	var TextBuffer string
	var DocName string

	pageInfo := struct {
		PageName       string `json:"pagename"`
		PageLocation   string `json:"href"`
		FirstHeading   string `json:"firstHeading"`
		FirstImage     string `json:"firstImage"`
		FirstParagraph string `json:"firstParagraph"`
	}{}

	TextBuffer = `{"pages":[`
	for _, fileName := range PageList {
		DocName = malkovich.FolderNameToDocName(fileName)
		pageInfo.PageName = DocName
		pageInfo.PageLocation = GetDocLink(fileName)
		pageInfo.FirstHeading = ""
		pageInfo.FirstImage = ""
		pageInfo.FirstParagraph = ""

		DocName := fileName
		summaryFilePath := getDocPreviewFilePath(DocRoot, DocName)
		jsonData, err := readPageSummary(summaryFilePath)
		if err != nil {
			log.Info("ERROR1: " + err.Error())
		}
		if err == nil {
			err = json.Unmarshal(jsonData, &pageSummaryData)
			if err != nil {
				log.Info("ERROR2: " + err.Error())
			}
			if err == nil {
				pageInfo.FirstHeading = pageSummaryData.PageTitle
				pageInfo.FirstImage = pageSummaryData.FirstImage
				pageInfo.FirstParagraph = pageSummaryData.FirstParagraph
			}
		}

		jsonData, err = json.Marshal(pageInfo)
		if err != nil {
			panic(err.Error())
		}
		TextBuffer = TextBuffer + string(jsonData) + `,`
	}
	TextBuffer = strings.TrimSuffix(TextBuffer, ",")
	TextBuffer = TextBuffer + `]}`

	return TextBuffer, nil
}
예제 #3
0
func UpdatePageSummary(docName, PageFilePath, PreviewFilePath string) error {
	var pageSummaryData PageSummaryData

	html, err := readPageAsHtml(docName, PageFilePath)
	if err != nil {
		return err
	}
	htmlreader := bytes.NewReader(html)

	//htmlString := string(html)
	//log.Info("HtmlString " + htmlString)

	doc, err := gq.NewDocumentFromReader(htmlreader)
	if err != nil {
		return err
	}

	var SelectedNodes *gq.Selection

	//======= work out the document heading ========
	DocTitle := ""

	if DocTitle == "" {
		SelectedNodes = doc.Find("h1, h2, h3, h4").First()
		if len(SelectedNodes.Nodes) == 1 {
			DocTitle = strings.TrimSpace(SelectedNodes.Text())
		}
	}
	if DocTitle == "" {
		DocTitle = docName
	}
	//DocTitle = base64.StdEncoding.EncodeToString([]byte(DocTitle))
	DocTitle = malkovich.FolderNameToDocName(DocTitle)
	pageSummaryData.PageTitle = DocTitle

	//======== look for an image =========
	DocImage := ""
	SelectedNodes = doc.Find("img").First()
	if len(SelectedNodes.Nodes) == 1 {
		for _, nodeAttr := range SelectedNodes.Nodes[0].Attr {
			if nodeAttr.Key == "src" {
				DocImage = nodeAttr.Val
				break
			}
		}
	}
	//DocImage = base64.StdEncoding.EncodeToString([]byte(DocImage))
	pageSummaryData.FirstImage = DocImage

	//======== look for the first paragraph =========
	FirstParagraph := ""
	SelectedNodes = doc.Find("p").First()
	if len(SelectedNodes.Nodes) == 1 {
		FirstParagraph = strings.TrimSpace(SelectedNodes.Text())
	}
	//TODO:HIGH Maybe limit to a set number of charactors here.
	//FirstParagraph = base64.StdEncoding.EncodeToString([]byte(FirstParagraph))
	pageSummaryData.FirstParagraph = FirstParagraph

	jsonData, err := json.Marshal(pageSummaryData)
	if err != nil {
		return err
	}

	// TODO:MED what would the best file permissions be here?
	err = ioutil.WriteFile(PreviewFilePath, jsonData, os.FileMode(0644))
	if err != nil {
		panic(err.Error())
	}

	return nil
}