forked from davidrthorn/cross_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lof-code.gs
171 lines (122 loc) · 4.35 KB
/
lof-code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function createLoF() {
var cursor = getCursorIndex();
var labSettings = PropertiesService.getDocumentProperties().getProperty( 'cross_fig' );
var labText = labSettings ? toCap( labSettings.split( '_' )[ 2 ] ) : 'Figure ';
if ( updateDoc() === 'error' ) return;
var labCount = encodeLabel();
var position = deleteLoF() || cursor;
insertDummyLoF( labCount, labText, position );
var html = HtmlService.createTemplateFromFile( 'lof' ).evaluate();
html.setWidth( 250 ).setHeight( 90 );
DocumentApp.getUi().showModalDialog( html, 'Generating list of figures...' );
}
function getCursorIndex() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if ( !cursor ) return 0;
var element = cursor.getElement();
return element.getParent().getChildIndex(element);
}
function encodeLabel() {
var doc = DocumentApp.getActiveDocument();
var paragraphs = doc.getBody().getParagraphs();
var labCount = { 'fig': 0 };
var figDescs = '';
for ( var i = 0; i < paragraphs.length; i++ ) {
var text = paragraphs[ i ].editAsText();
var locs = getCrossLinks( text, 5 );
var start = locs[ 0 ][ 0 ];
var url = locs[ 2 ][ 0 ];
if ( !locs[ 0 ].length ) continue;
if ( url.substr( 0, 4 ) === '#fig' ) {
figDescs += 'ഛಎ' + text.getText().match(/([ ]\d[^\w]*)([^\.]*)/)[2]
text.deleteText( start, start + 1 )
.insertText( start, '☙' );
labCount[ 'fig' ]++;
}
}
PropertiesService.getDocumentProperties().setProperty('fig_descs', figDescs)
return labCount;
}
function deleteLoF() {
var lofTable = findLoF();
if ( !lofTable ) return;
var lofIndex = lofTable.getParent().getChildIndex( lofTable );
lofTable.removeFromParent();
return lofIndex
}
function findLoF() {
var lof = DocumentApp.getActiveDocument().getNamedRanges( 'lofTable' )[ 0 ];
return lof ? lof.getRange().getRangeElements()[ 0 ].getElement().asTable() : null;
}
function insertDummyLoF( labCount, labText, position ) {
var doc = DocumentApp.getActiveDocument();
var lofCells = [];
var labText = toCap( labText );
var placeholder = '...';
var range = doc.newRange();
doc.getNamedRanges( 'lofTable' ).forEach( function( r ) {
r.remove()
});
var figDescs = PropertiesService.getDocumentProperties().getProperty('fig_descs');
var splitDescs = figDescs ? figDescs.split('ഛಎ') : null;
for ( var i = 1; i <= labCount[ 'fig' ]; i++ ) {
var figName = labText + i;
var figDesc = splitDescs && splitDescs[i].length ? ': ' + splitDescs[i] : '';
var row = [ figName + figDesc, placeholder ];
lofCells.push( row );
}
var lofTable = doc.getBody().insertTable( position, lofCells )
styleLoF( lofTable );
range.addElement( lofTable );
doc.addNamedRange('lofTable', range.build() )
}
function styleLoF( lofTable ) {
lofTable.setBorderWidth( 0 );
var styleAttributes = {
'BOLD': null,
'ITALIC': null,
'UNDERLINE': null,
'FONT_SIZE': null
};
for ( var i = lofTable.getNumRows(); i--; ) {
var row = lofTable.getRow( i );
lofTable.setAttributes( styleAttributes ).setColumnWidth( 1, 64 );
row.getCell( 0 ).setPaddingLeft( 0 );
row.getCell( 1 ).setPaddingRight( 0 )
.getChild( 0 ).asParagraph().setAlignment( DocumentApp.HorizontalAlignment.RIGHT );
}
}
function getDocAsPDF() {
return DocumentApp.getActiveDocument().getBlob().getBytes();
}
function insertLoFNumbers( pg_nums ) {
var lofTable = findLoF();
var currentRow = 0;
for ( var i = 0; i < pg_nums.length; i++ ) {
var labCount = pg_nums[ i ];
if ( !labCount ) continue;
for ( var j = currentRow; j < lofTable.getNumRows(); j++ ) {
lofTable.getCell( j, 1 )
.clear()
.getChild( 0 ).asParagraph().appendText( i + 1 );
}
var currentRow = currentRow + labCount;
}
}
function restoreLabels() {
var doc = DocumentApp.getActiveDocument();
var paras = doc.getBody().getParagraphs();
for ( var i = 0; i < paras.length; i++ ) {
var text = paras[ i ].editAsText();
var locs = getCrossLinks( text, 5 );
var starts = locs[ 0 ];
var urls = locs[ 2 ];
if ( !starts.length ) continue;
for ( var k = starts.length; k--; ) {
var start = starts[ k ];
var url = urls[ k ];
if (url.substr( 0, 4 ) === '#fig') text.deleteText( start - 1, start );
}
}
updateDoc();
}