view cmd_pdf.go @ 14:bac5eb544d4d

fix
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 01 Apr 2020 19:10:30 +0900
parents 989cfda07d71
children 9014a505c54c
line wrap: on
line source

package lectable

import (
	"bufio"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path/filepath"
)

type cmdPDF struct{}

func (cp *cmdPDF) name() string {
	return "pdf"
}

func (cd *cmdPDF) description() string {
	return "parse from pdf"
}

func (cd *cmdPDF) run(ctx context.Context, argv []string, outStream, errStream io.Writer) error {
	outputDir := guessOutputDir()
	err := checkAndMkdirBuilddir(outputDir)
	if err != nil {
		return err
	}

	ppsr, err := convertStringFromPDF(argv)
	if err != nil {
		return err
	}
	var fpfs []*lectureFPDF
	for _, pps := range *ppsr {
		for _, pp := range pps {
			fpdf, err := str2lectureFPDF(pp)
			if err != nil {
				return err
			}
			fpfs = append(fpfs, fpdf...)
		}
	}

	var dlfps []DumpLectureFPDF

	for _, h := range fpfs {
		dlfps = append(dlfps, DumpLectureFPDF{
			Id:       h.id,
			IsSelect: h.isSelect,
			Place:    h.place,
			Grades:   h.grades,
			Day:      h.day,
		})
	}
	fmt.Println(dlfps)
	dumpJson(dlfps, outputDir)
	return nil
}

type DumpLectureFPDF struct {
	Id       string `json:id`
	IsSelect bool   `json:isSelect`
	Place    string `json:place`
	Day      string `json:day`
	Grades   []int  `json:grades`
}

func dumpJson(dlfp []DumpLectureFPDF, outputdir string) error {
	bytes, err := json.Marshal(dlfp)
	if err != nil {
		return err
	}
	fp := filepath.Join(outputdir, "dumpFromPDF.json")
	file, err := os.Create(fp)
	bw := bufio.NewWriter(file)
	_, err = bw.Write(bytes)
	if err != nil {
		return err
	}
	bw.Flush()
	file.Close()
	return nil
}