<commit_before>package code2docx

import (
	"bufio"
	"log"
	"os"
	"strings"

	"baliance.com/gooxml/document"
	"baliance.com/gooxml/schema/soo/wml"
)

type Outer struct {
	Config  *Config
	Base    string
	Files   []string
	Docfile string
	doc     *document.Document
}

func (o *Outer) WriteDoc() error {
	o.init()
	for _, file := range o.Files {

		filename := strings.Replace(file, o.Base, "", 1)

		o.writeToDoc(filename, file)

	}
	o.doc.SaveToFile(o.Docfile)
	return nil
}

func (o *Outer) init() {
	doc := document.New()
	o.doc = doc
	hdr := doc.AddHeader()
	para := hdr.AddParagraph()
	setParagraphAlignCenter(para)
	para.AddRun().AddText(o.Config.Header)
	doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)

	para = doc.AddParagraph()
	para.SetStyle("Title")
	setParagraphAlignCenter(para)
	para.AddRun().AddText(o.Config.Title)

	para = doc.AddParagraph()
	para.AddRun().AddText("")
}

func ensurePPr(p *wml.CT_P) {
	if p.PPr == nil {
		p.PPr = wml.NewCT_PPr()
	}
}
func setParagraphAlignCenter(para document.Paragraph) {
	paraAlign := wml.NewCT_Jc()
	paraAlign.ValAttr = wml.ST_JcCenter
	x := para.X()
	ensurePPr(x)
	x.PPr.Jc = paraAlign
}

func (o *Outer) writeToDoc(filename, filepath string) {
	doc := o.doc
	para := doc.AddParagraph()
	run := para.AddRun()
	run.AddText(filename + ":")
	run.AddBreak()

	fio, err := os.Open(filepath)
	if err != nil {
		log.Fatal("open file error: ", err)
	}
	defer fio.Close()
	fbscaner := bufio.NewScanner(fio)
	for fbscaner.Scan() {
		para := doc.AddParagraph()
		run := para.AddRun()
		run.AddText(fbscaner.Text())
	}
	if err := fbscaner.Err(); err != nil {
		log.Fatal("read file error: ", err)
	}

	para = doc.AddParagraph()
	run = para.AddRun()
	run.AddBreak()
}<commit_msg>refactor<commit_after>package code2docx

import (
	"bufio"
	"log"
	"os"
	"strings"

	"baliance.com/gooxml/document"
	"baliance.com/gooxml/schema/soo/wml"
)

type Outer struct {
	Config  *Config
	Base    string
	Files   []string
	Docfile string
	doc     *document.Document
}

func (o *Outer) WriteDoc() error {
	o.init()
	for _, file := range o.Files {

		filename := strings.Replace(file, o.Base, "", 1)

		o.writeToDoc(filename, file)

	}
	o.doc.SaveToFile(o.Docfile)
	return nil
}

func (o *Outer) init() {
	doc := document.New()
	o.doc = doc
	hdr := doc.AddHeader()
	para := hdr.AddParagraph()
	setParagraphAlignCenter(para)
	para.AddRun().AddText(o.Config.Header)
	doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)

	para = doc.AddParagraph()
	para.SetStyle("Title")
	setParagraphAlignCenter(para)
	para.AddRun().AddText(o.Config.Title)

	para = doc.AddParagraph()
	para.AddRun().AddText("")
}

func ensurePPr(para document.Paragraph) {
	if para.X().PPr == nil {
		para.X().PPr = wml.NewCT_PPr()
	}
}
func setParagraphAlignCenter(para document.Paragraph) {
	ensurePPr(para)
	paraAlign := wml.NewCT_Jc()
	paraAlign.ValAttr = wml.ST_JcCenter
	para.X().PPr.Jc = paraAlign
}

func (o *Outer) writeToDoc(filename, filepath string) {
	doc := o.doc
	para := doc.AddParagraph()
	run := para.AddRun()
	run.AddText(filename + ":")
	run.AddBreak()

	fio, err := os.Open(filepath)
	if err != nil {
		log.Fatal("open file error: ", err)
	}
	defer fio.Close()
	fbscaner := bufio.NewScanner(fio)
	for fbscaner.Scan() {
		para := doc.AddParagraph()
		run := para.AddRun()
		run.AddText(fbscaner.Text())
	}
	if err := fbscaner.Err(); err != nil {
		log.Fatal("read file error: ", err)
	}

	para = doc.AddParagraph()
	run = para.AddRun()
	run.AddBreak()
}
{'fraction_non_alphanumeric': 0.16484464902186421, 'fraction_numerical': 0.0011507479861910242, 'mean_word_length': 3.988522238163558, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2420842', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1421, 'n_words': 266}