1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 15:49:44 +02:00

page numbering engine

This commit is contained in:
Harvey Kandola 2017-12-10 14:05:23 +00:00
parent 5de856e579
commit c5c988709d
9 changed files with 198 additions and 26 deletions

74
model/page/numbering.go Normal file
View file

@ -0,0 +1,74 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package page
import (
"fmt"
)
// Numberize calculates numbers for pages, e.g. 1, 1.1, 2.2.1
// that form a document's Table of Contents.
//
func Numberize(pages []Page) {
// index := 1
var prevPageLevel uint64
parts := []int{1} // we store 1, 1, 2, and then generate numbering as "1.1.2"
for i, p := range pages {
// handle bad data
if p.Level == 0 {
p.Level = 1
}
// increment level numbers each time we encounter age level of 1
// if p.Level == 1 {
// index++
// parts = []int{index}
// } else if p.Level == prevPageLevel {
// parts[len(parts)-1]++
// }
if i != 0 {
// we ...
if p.Level > prevPageLevel {
parts = append(parts, 1)
}
if p.Level == prevPageLevel {
parts[len(parts)-1]++
}
if p.Level < prevPageLevel {
end := (prevPageLevel - p.Level)
if int(end) > len(parts) {
end = uint64(len(parts))
}
parts = parts[0 : len(parts)-int(end)]
parts[len(parts)-1]++
}
}
// generate numbering for page using parts array
numbering := ""
for i, v := range parts {
dot := ""
if i != len(parts)-1 {
dot = "."
}
numbering = fmt.Sprintf("%s%d%s", numbering, v, dot)
}
pages[i].Numbering = numbering
// update state
prevPageLevel = p.Level
}
}

View file

@ -0,0 +1,114 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package page
import (
"testing"
)
func TestNumberize1(t *testing.T) {
pages := []Page{}
pages = append(pages, Page{Level: 1, Sequence: 1000})
pages = append(pages, Page{Level: 2, Sequence: 2000})
pages = append(pages, Page{Level: 2, Sequence: 3000})
pages = append(pages, Page{Level: 2, Sequence: 4000})
pages = append(pages, Page{Level: 3, Sequence: 5000})
pages = append(pages, Page{Level: 3, Sequence: 6000})
pages = append(pages, Page{Level: 4, Sequence: 7000})
pages = append(pages, Page{Level: 4, Sequence: 8000})
pages = append(pages, Page{Level: 5, Sequence: 9000})
pages = append(pages, Page{Level: 3, Sequence: 10000})
pages = append(pages, Page{Level: 2, Sequence: 11000})
Numberize(pages)
expecting := []string{
"1",
"1.1",
"1.2",
"1.3",
"1.3.1",
"1.3.2",
"1.3.2.1",
"1.3.2.2",
"1.3.2.2.1",
"1.3.3",
"1.4",
}
for i, p := range pages {
if p.Numbering != expecting[i] {
t.Errorf("(Test 1) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering)
}
}
}
func TestNumberize2(t *testing.T) {
pages := []Page{}
pages = append(pages, Page{Level: 1, Sequence: 1000})
pages = append(pages, Page{Level: 1, Sequence: 2000})
pages = append(pages, Page{Level: 1, Sequence: 3000})
pages = append(pages, Page{Level: 1, Sequence: 4000})
pages = append(pages, Page{Level: 1, Sequence: 5000})
pages = append(pages, Page{Level: 1, Sequence: 6000})
Numberize(pages)
expecting := []string{
"1",
"2",
"3",
"4",
"5",
"6",
}
for i, p := range pages {
if p.Numbering != expecting[i] {
t.Errorf("(Test 2) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering)
}
}
}
func TestNumberize3(t *testing.T) {
pages := []Page{}
pages = append(pages, Page{Level: 0, Sequence: 1000})
pages = append(pages, Page{Level: 1, Sequence: 2000})
pages = append(pages, Page{Level: 2, Sequence: 3000})
pages = append(pages, Page{Level: 3, Sequence: 4000})
pages = append(pages, Page{Level: 4, Sequence: 4000})
pages = append(pages, Page{Level: 1, Sequence: 5000})
pages = append(pages, Page{Level: 2, Sequence: 6000})
Numberize(pages)
expecting := []string{
"1",
"2",
"2.1",
"2.1.1",
"2.1.1.1",
"3",
"3.1",
}
for i, p := range pages {
if p.Numbering != expecting[i] {
t.Errorf("(Test 3) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering)
}
}
}
// go test github.com/documize/community/core/model -run TestNumberize

View file

@ -29,6 +29,7 @@ type Page struct {
BlockID string `json:"blockId"`
Level uint64 `json:"level"`
Sequence float64 `json:"sequence"`
Numbering string `json:"numbering"`
Title string `json:"title"`
Body string `json:"body"`
Revisions uint64 `json:"revisions"`
@ -102,7 +103,6 @@ type NewPage struct {
Meta Meta `json:"meta"`
}
// PageSequenceRequest details a page ID and its sequence within the document.
type PageSequenceRequest struct {
PageID string `json:"pageId"`
@ -113,4 +113,4 @@ type PageSequenceRequest struct {
type PageLevelRequest struct {
PageID string `json:"pageId"`
Level int `json:"level"`
}
}