github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process_clean.go (about)

     1  /*
     2     Golang To Github Markdown Utility: gotomd
     3     Copyright (C) 2023, 2024 Leslie Dancsecs
     4  
     5     This program is free software: you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation, either version 3 of the License, or
     8     (at your option) any later version.
     9  
    10     This program is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13     GNU General Public License for more details.
    14  
    15     You should have received a copy of the GNU General Public License
    16     along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17  */
    18  
    19  package main
    20  
    21  import (
    22  	"bytes"
    23  	"log"
    24  	"os"
    25  	"path/filepath"
    26  )
    27  
    28  func cleanMD(rPath string) error {
    29  	var (
    30  		err         error
    31  		rDir, rFile string
    32  		wDir, wFile string
    33  		wPath       string
    34  		fileBytes   []byte
    35  		res         string
    36  	)
    37  
    38  	rDir, rFile = filepath.Split(rPath)
    39  	wDir = rDir
    40  
    41  	if outputDir != "." {
    42  		wDir = outputDir
    43  	}
    44  
    45  	wFile = rFile + ".gtm"
    46  	wPath = filepath.Join(wDir, wFile)
    47  
    48  	if verbose {
    49  		log.Printf("Cleaning %s to: %s", rPath, wPath)
    50  	}
    51  
    52  	fileBytes, err = os.ReadFile(rPath) //nolint:gosec // Ok.
    53  
    54  	if err == nil {
    55  		fileData := string(bytes.TrimRight(fileBytes, "\n"))
    56  		res, err = cleanMarkDownDocument(fileData)
    57  	}
    58  
    59  	if err == nil {
    60  		err = writeFile(wPath, res)
    61  	}
    62  
    63  	return err
    64  }