Skip to content

Commit

Permalink
Refactor writing to tmpfile code (#4)
Browse files Browse the repository at this point in the history
* Write to tmpfile refactored
  • Loading branch information
MichelBoucey authored Jul 16, 2022
1 parent 7101254 commit 610fc6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`dumber`, a command line tool for **d**(igital n)**umber**(ing) Markdown document sections.

With `dumber` (you already use `git`, don't you?) you can add or remove section numbers to header sections of your Mardown files (works on hash sign only).
With `dumber` (you already use `git`, don't you?) you can *add* or *remove* section numbers to header sections of your Mardown files (works on hash sign only).

## Usage

Expand Down
40 changes: 19 additions & 21 deletions cmd/dumber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
Expand All @@ -36,7 +36,7 @@ import (

func main() {

version := "1.1.1"
version := "1.1.2"

var headerCounters [7]int
var mdTmpFile *os.File
Expand Down Expand Up @@ -120,33 +120,20 @@ func main() {
rewrittenLine = header + " " + section + " " + title
}

if *writeFlag {
_, err := io.WriteString(mdTmpFile, rewrittenLine+newLine)
if err != nil {
panic(err)
}
} else {
fmt.Println(rewrittenLine)
}

section = ""
WriteTmpFile(*writeFlag, mdTmpFile, rewrittenLine, newLine)

for i := currentHeaderType + 1; i <= 6; i++ {
headerCounters[i] = 0
}

section = ""

} else {

if *writeFlag {
_, err := io.WriteString(mdTmpFile, line+newLine)
if err != nil {
panic(err)
}
} else {
fmt.Println(line)
}
WriteTmpFile(*writeFlag, mdTmpFile, line, newLine)

}

}
if err := scanner.Err(); err != nil {
log.Fatal(err)
Expand All @@ -164,6 +151,17 @@ func main() {
}
}

func WriteTmpFile(wf bool, tf *os.File, l string, nl string) {
if wf {
_, e := io.WriteString(tf, l+nl)
if e != nil {
panic(e)
}
} else {
fmt.Println(l)
}
}

func AddSectionChunk(s *string, hc int, cht int, ht int) {
if hc > 0 && cht >= ht {
*s += strconv.Itoa(hc) + "."
Expand Down

0 comments on commit 610fc6c

Please sign in to comment.