mirror of
https://github.com/documize/community.git
synced 2025-07-20 21:59:42 +02:00
page numbering engine
This commit is contained in:
parent
5de856e579
commit
c5c988709d
9 changed files with 198 additions and 26 deletions
|
@ -13,24 +13,6 @@ package database
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
//
|
|
||||||
// // go test github.com/documize/community/core/database -run TestVersionExtract
|
|
||||||
// func TestVersionExtract(t *testing.T) {
|
|
||||||
// ts(t, "5", 5)
|
|
||||||
// ts(t, "45-0ubuntu0-12.12.3", 45)
|
|
||||||
// ts(t, "untu0-12.12.3", 0)
|
|
||||||
// ts(t, "junk-string", 0)
|
|
||||||
// ts(t, "somethingstring", 0)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// func ts(t *testing.T, in string, out int) {
|
|
||||||
// got := ExtractVersionNumber(in)
|
|
||||||
//
|
|
||||||
// if got != out {
|
|
||||||
// t.Errorf("version input `%s` got `%d` expected `%d`\n", in, got, out)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// go test github.com/documize/community/core/database -run TestGetVersion
|
// go test github.com/documize/community/core/database -run TestGetVersion
|
||||||
func TestGetVersion(t *testing.T) {
|
func TestGetVersion(t *testing.T) {
|
||||||
ts2(t, "5.7.10", []int{5, 7, 10})
|
ts2(t, "5.7.10", []int{5, 7, 10})
|
||||||
|
|
|
@ -191,7 +191,7 @@ func (h *Handler) GetPage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// GetPages gets all pages for document.
|
// GetPages gets all pages for document.
|
||||||
func (h *Handler) GetPages(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) GetPages(w http.ResponseWriter, r *http.Request) {
|
||||||
method := "page.GetPage"
|
method := "page.GetPages"
|
||||||
ctx := domain.GetRequestContext(r)
|
ctx := domain.GetRequestContext(r)
|
||||||
|
|
||||||
documentID := request.Param(r, "documentID")
|
documentID := request.Param(r, "documentID")
|
||||||
|
@ -219,6 +219,8 @@ func (h *Handler) GetPages(w http.ResponseWriter, r *http.Request) {
|
||||||
pages = []page.Page{}
|
pages = []page.Page{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
page.Numberize(pages)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteServerError(w, method, err)
|
response.WriteServerError(w, method, err)
|
||||||
h.Runtime.Log.Error(method, err)
|
h.Runtime.Log.Error(method, err)
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
// https://documize.com
|
// https://documize.com
|
||||||
|
|
||||||
import { computed } from '@ember/object';
|
import { computed } from '@ember/object';
|
||||||
|
|
||||||
import Component from '@ember/component';
|
import Component from '@ember/component';
|
||||||
import TooltipMixin from '../../../mixins/tooltip';
|
import TooltipMixin from '../../../mixins/tooltip';
|
||||||
|
|
||||||
|
@ -102,7 +101,7 @@ export default Component.extend(TooltipMixin, {
|
||||||
this.set('codeEditor', null);
|
this.set('codeEditor', null);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.destroyTooltips();
|
this.removeT0oltips();
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ export default Component.extend(SectionMixin, NotifierMixin, TooltipMixin, {
|
||||||
},
|
},
|
||||||
|
|
||||||
willDestroyElement() {
|
willDestroyElement() {
|
||||||
this.destroyTooltips();
|
this.removeTooltips();
|
||||||
},
|
},
|
||||||
|
|
||||||
getBoardLists() {
|
getBoardLists() {
|
||||||
|
|
|
@ -21,13 +21,14 @@ export default Model.extend({
|
||||||
pageType: attr('string'),
|
pageType: attr('string'),
|
||||||
level: attr('number', { defaultValue: 1 }),
|
level: attr('number', { defaultValue: 1 }),
|
||||||
sequence: attr('number', { defaultValue: 0 }),
|
sequence: attr('number', { defaultValue: 0 }),
|
||||||
|
numbering: attr('string'),
|
||||||
revisions: attr('number', { defaultValue: 0 }),
|
revisions: attr('number', { defaultValue: 0 }),
|
||||||
blockId: attr('string'),
|
blockId: attr('string'),
|
||||||
title: attr('string'),
|
title: attr('string'),
|
||||||
body: attr('string'),
|
body: attr('string'),
|
||||||
rawBody: attr('string'),
|
rawBody: attr('string'),
|
||||||
meta: attr(),
|
meta: attr(),
|
||||||
|
|
||||||
tagName: computed('level', function () {
|
tagName: computed('level', function () {
|
||||||
return "h2";
|
return "h2";
|
||||||
// return "h" + (this.get('level') + 1);
|
// return "h" + (this.get('level') + 1);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<div class="col-10">
|
<div class="col-10">
|
||||||
<div class="document-structure">
|
<div class="document-structure">
|
||||||
<div class="page-header">
|
<div class="page-header">
|
||||||
<span class="page-number">1</span>
|
<span class="page-number">{{page.numbering}}</span>
|
||||||
<span class="page-title">{{page.title}}</span>
|
<span class="page-title">{{page.title}}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
74
model/page/numbering.go
Normal file
74
model/page/numbering.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
114
model/page/numbering_test.go
Normal file
114
model/page/numbering_test.go
Normal 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
|
|
@ -29,6 +29,7 @@ type Page struct {
|
||||||
BlockID string `json:"blockId"`
|
BlockID string `json:"blockId"`
|
||||||
Level uint64 `json:"level"`
|
Level uint64 `json:"level"`
|
||||||
Sequence float64 `json:"sequence"`
|
Sequence float64 `json:"sequence"`
|
||||||
|
Numbering string `json:"numbering"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
Revisions uint64 `json:"revisions"`
|
Revisions uint64 `json:"revisions"`
|
||||||
|
@ -102,7 +103,6 @@ type NewPage struct {
|
||||||
Meta Meta `json:"meta"`
|
Meta Meta `json:"meta"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// PageSequenceRequest details a page ID and its sequence within the document.
|
// PageSequenceRequest details a page ID and its sequence within the document.
|
||||||
type PageSequenceRequest struct {
|
type PageSequenceRequest struct {
|
||||||
PageID string `json:"pageId"`
|
PageID string `json:"pageId"`
|
||||||
|
@ -113,4 +113,4 @@ type PageSequenceRequest struct {
|
||||||
type PageLevelRequest struct {
|
type PageLevelRequest struct {
|
||||||
PageID string `json:"pageId"`
|
PageID string `json:"pageId"`
|
||||||
Level int `json:"level"`
|
Level int `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue