-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteDocx.jl
1987 lines (1693 loc) · 62.3 KB
/
WriteDocx.jl
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module WriteDocx
import EzXML as E
using EnumX: @enumx
using MacroTools: @capture
using ZipFile: ZipFile
using OrderedCollections: OrderedDict
"""
Length
The supertype for all length metrics that WriteDocx can handle.
Each `Length` can be converted to any other `Length` and can therefore
be passed to any struct that stores a specific length type.
"""
abstract type Length end
"""
Point(value::Float64) <: Length
A length of one typographic point, or 1/72 of an inch.
For convenience, the constant `pt` is provided for `Point(1)`.
"""
struct Point <: Length
value::Float64
end
const pt = Point(1)
"""
HalfPoint(value::Float64) <: Length
A length of a half typographic point, or 1/144 of an inch.
For convenience, the constant `halfpt` is provided for `HalfPoint(1)`.
"""
struct HalfPoint <: Length
value::Float64
end
const halfpt = HalfPoint(1)
"""
EighthPoint(value::Float64) <: Length
A length of one eight of a typographic point, or 1/576 of an inch.
For convenience, the constant `eighthpt` is provided for `EighthPoint(1)`.
"""
struct EighthPoint <: Length
value::Float64
end
const eighthpt = EighthPoint(1)
"""
Twip(value::Float64) <: Length
A length of one twip, or twentieth of a point, or 1/1440 of an inch.
For convenience, the constant `twip` is provided for `Twip(1)`.
"""
struct Twip <: Length
value::Float64
end
const twip = Twip(1)
"""
EMU(value::Float64) <: Length
A length of one English metric unit, or 1/914400 of an inch.
For convenience, the constant `emu` is provided for `EMU(1)`.
"""
struct EMU <: Length
value::Float64
end
const emu = EMU(1)
"""
Inch(value::Float64) <: Length
A length of one inch.
For convenience, the constant `inch` is provided for `Inch(1)`.
"""
struct Inch <: Length
value::Float64
end
const inch = Inch(1)
"""
Centimeter(value::Float64) <: Length
A length of one centimeter, or 1/2.54 of an inch.
For convenience, the constants `cm` and `mm` are provided for
`Centimeter(1)` and `Centimeter(0.1)`, respectively.
"""
struct Centimeter <: Length
value::Float64
end
const mm = Centimeter(0.1)
const cm = Centimeter(1)
inchfactor(::Type{Inch}) = 1.0
inchfactor(::Type{Point}) = 72.0
inchfactor(::Type{HalfPoint}) = 144.0
inchfactor(::Type{EighthPoint}) = 576.0
inchfactor(::Type{Twip}) = 20.0 * 72.0
inchfactor(::Type{EMU}) = 914400.0
inchfactor(::Type{Centimeter}) = 2.54
# explicit definitions due to ambiguities with (T::Type{<:Length})(...)
Point(l::Length) = convert(Point, l)
HalfPoint(l::Length) = convert(HalfPoint, l)
EighthPoint(l::Length) = convert(EighthPoint, l)
Twip(l::Length) = convert(Twip, l)
EMU(l::Length) = convert(EMU, l)
Inch(l::Length) = convert(Inch, l)
Centimeter(l::Length) = convert(Centimeter, l)
Base.convert(::Type{L}, l::L) where L <: Length = l
Base.convert(::Type{L1}, l::L2) where {L1 <: Length, L2 <: Length} = L1(inchfactor(L1) * l.value / inchfactor(L2))
Base.isless(l1::L1, l2::L2) where {L1 <: Length, L2 <: Length} = convert(L2, l1).value < l2.value
Base.:(*)(x::Real, l::L) where L <: Length = L(l.value * x)
Base.:(*)(l::L, x::Real) where L <: Length = x * l
Base.:(/)(l::L, l2::Length) where L <: Length = l.value / convert(L, l2).value
Base.:(/)(l::L, r::Real) where L <: Length = L(l.value / r)
# it doesn't matter if return types here are order dependent, this is just for convenience anyway
Base.:(+)(l::L, l2::Length) where L <: Length = L(l.value + convert(L, l2).value)
Base.:(-)(l::L, l2::Length) where L <: Length = L(l.value - convert(L, l2).value)
const Maybe = Union{Nothing, <:Any}
macro partialkw(expr::Expr)
if expr.head === :struct
name::Symbol = let
maybe_parametric = if @capture expr.args[2] x_ <: y_
x
else
expr.args[2]
end
if @capture maybe_parametric x_{y_}
x
else
maybe_parametric
end
end
fields = expr.args[3].args
kwargs_boundary = findfirst(e -> (@capture e @kwargs), fields)
kwargs_boundary = something(kwargs_boundary, length(fields) + 1)
positional = fields[1:kwargs_boundary-1]
kw = fields[kwargs_boundary+1:end]
stripped_kw = map(kw) do field
if @capture field x_ = y_
x
else
field
end
end
new_struct = Expr(
:struct,
expr.args[1],
expr.args[2],
Expr(:block, positional..., stripped_kw...)
)
kw_transformed = map(filter(x -> !(x isa LineNumberNode), kw)) do kw
if @capture kw x_ = y_
if !@capture x sym_::typ_
sym = x
end
Expr(:kw, sym::Symbol, y)
else
if @capture kw sym_::typ_
sym::Symbol
else
kw::Symbol
end
end
end
positional_syms = map(filter(x -> !(x isa LineNumberNode), positional)) do arg
if !@capture arg sym_::typ_
sym = arg
end
sym::Symbol
end
kw_syms = map(kw_transformed) do kw
kw isa Symbol ? kw : kw.args[1]::Symbol
end
func = Expr(
:function,
Expr(
:call,
name,
Expr(
:parameters,
kw_transformed...
),
positional_syms...
),
quote
$(name)($(positional_syms...), $(kw_syms...))
end
)
result = quote
$new_struct
$func
end
esc(result)
else
error("Expected struct.")
end
end
struct Text
text::String
end
is_inline_element(_) = false
is_inline_element(::Type{Text}) = true
@enumx BreakType column page text_wrapping
struct Break
type::BreakType.T
end
is_inline_element(::Type{Break}) = true
Break() = Break(BreakType.text_wrapping)
struct Size
size::HalfPoint
end
"""
VerticalAlignment
An enum that can be either `baseline`, `subscript` or `superscript`.
"""
@enumx VerticalAlignment baseline subscript superscript
"""
UnderlinePattern
An enum that can be either `dash`, `dash_dot_dot_heavy`, `dash_dot_heavy`, `dashed_heavy`, `dash_long`, `dash_long_heavy`, `dot_dash`, `dot_dot_dash`, `dotted`, `dotted_heavy`, `double`, `none`, `single`, `thick`, `wave`, `wavy_double`, `wavy_heavy` or `words`.
"""
@enumx UnderlinePattern dash dash_dot_dot_heavy dash_dot_heavy dashed_heavy dash_long dash_long_heavy dot_dash dot_dot_dash dotted dotted_heavy double none single thick wave wavy_double wavy_heavy words
"""
BorderStyle
An enum that can be either `single`, `dash_dot_stroked`, `dashed`, `dash_small_gap`, `dot_dash`, `dot_dot_dash`, `dotted`, `double`, `double_wave`, `inset`, `nil`, `none`, `outset`, `thick`, `thick_thin_large_gap`, `thick_thin_medium_gap`, `thick_thin_small_gap`, `thin_thick_large_gap`, `thin_thick_medium_gap`, `thin_thick_small_gap`, `thin_thick_thin_large_gap`, `thin_thick_thin_medium_gap`, `thin_thick_thin_small_gap`, `three_d_emboss`, `three_d_engrave`, `triple` or `wave`
"""
@enumx BorderStyle single dash_dot_stroked dashed dash_small_gap dot_dash dot_dot_dash dotted double double_wave inset nil none outset thick thick_thin_large_gap thick_thin_medium_gap thick_thin_small_gap thin_thick_large_gap thin_thick_medium_gap thin_thick_small_gap thin_thick_thin_large_gap thin_thick_thin_medium_gap thin_thick_thin_small_gap three_d_emboss three_d_engrave triple wave
snake_to_camel(string::String) = replace(string, r"_[a-z]" => uppercase ∘ last)
"""
Fonts(; [ascii::String, high_ansi::String, complex::String, east_asia::String])
Fonts(font; kwargs...)
Specifies fonts to use for four different Unicode character ranges.
The convenience constructor with one positional argument changes the font for
`ascii` and `high_ansi`, which should usually be the same.
"""
Base.@kwdef struct Fonts
ascii::Maybe{String} = nothing
high_ansi::Maybe{String} = nothing
complex::Maybe{String} = nothing
east_asia::Maybe{String} = nothing
end
Fonts(font; kwargs...) = Fonts(; ascii = font, high_ansi = font, kwargs...)
"""
HexColor(s::String)
A color in hexadecimal RGB format, for example "FF0000" for red or
"333333" for a dark gray.
"""
struct HexColor
hex::String
function HexColor(s::String)
if match(r"^[a-fA-F0-9]{6}$", s) === nothing
error("Invalid color string $(repr(s)).")
end
new(s)
end
end
struct Automatic end
const automatic = Automatic()
"""
AutomaticDefault{T}
Signals that either a value of type `T` is accepted or
`automatic`, for which the viewer application chooses appropriate
behavior.
"""
struct AutomaticDefault{T}
value::Union{Automatic, T}
end
struct Bold end
struct Italic end
Base.convert(::Type{AutomaticDefault{X}}, x::X) where X = AutomaticDefault{X}(x)
Base.convert(::Type{AutomaticDefault{X}}, ::Automatic) where X = AutomaticDefault{X}(automatic)
struct Underline
color::AutomaticDefault{HexColor}
pattern::UnderlinePattern.T
end
struct Color
color::AutomaticDefault{HexColor}
end
struct ParagraphStyle
name::String
end
struct RunStyle
name::String
end
struct GridSpan
n::Int
end
struct VerticalMerge
restart::Bool
end
"""
TableCellBorder(; kwargs...)
Holds properties for one border of a table cell and is used by [`TableCellBorders`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `color::`[`AutomaticDefault`](@ref)`{`[`HexColor`]@ref`}` | The color of the border. |
| `shadow::Bool` | Applies a shadow effect if `true`. |
| `space::`[`Point`](@ref) | The spacing between border and content. |
| `size::`[`EighthPoint`](@ref) | The thickness of the border line. |
| `style::`[`BorderStyle`](@ref)`.T` | The line style of the border. |
"""
Base.@kwdef struct TableCellBorder
color::Maybe{AutomaticDefault{HexColor}} = nothing
shadow::Maybe{Bool} = nothing
space::Maybe{Point} = nothing
size::Maybe{EighthPoint} = nothing
style::Maybe{BorderStyle.T}
end
"""
ParagraphBorder(; kwargs...)
Holds properties for one border of a table cell and is used by [`ParagraphBorders`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `color::`[`AutomaticDefault`](@ref)`{`[`HexColor`]@ref`}` | The color of the border. |
| `shadow::Bool` | Applies a shadow effect if `true`. |
| `space::`[`Point`](@ref) | The spacing between border and content. |
| `size::`[`EighthPoint`](@ref) | The thickness of the border line. |
| `style::`[`BorderStyle`](@ref)`.T` | The line style of the border. |
"""
Base.@kwdef struct ParagraphBorder
color::Maybe{AutomaticDefault{HexColor}} = nothing
shadow::Maybe{Bool} = nothing
space::Maybe{Point} = nothing
size::Maybe{EighthPoint} = nothing
style::Maybe{BorderStyle.T}
end
"""
TableCellBorders(; kwargs...)
Holds properties for the borders of a [`TableCell`](@ref) and is used by [`TableCellProperties`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `top::`[`TableCellBorder`](@ref) | The properties of the top border. |
| `bottom::`[`TableCellBorder`](@ref) | The properties of the bottom border. |
| `start::`[`TableCellBorder`](@ref) | The properties of the left border in left-to-right text. |
| `stop::`[`TableCellBorder`](@ref) | The properties of the right border in left-to-right text. |
| `inside_h::`[`TableCellBorder`](@ref) | The properties of the horizontal border that lies between adjacent cells. |
| `inside_v::`[`TableCellBorder`](@ref) | The properties of the vertical border that lies between adjacent cells. |
| `tl2br::`[`TableCellBorder`](@ref) | The properties of the diagonal border going from the top left to the bottom right corner. |
| `tr2bl::`[`TableCellBorder`](@ref) | The properties of the diagonal border going from the top right to the bottom left corner. |
"""
Base.@kwdef struct TableCellBorders
top::Maybe{TableCellBorder} = nothing
start::Maybe{TableCellBorder} = nothing
bottom::Maybe{TableCellBorder} = nothing
stop::Maybe{TableCellBorder} = nothing
inside_h::Maybe{TableCellBorder} = nothing
inside_v::Maybe{TableCellBorder} = nothing
tl2br::Maybe{TableCellBorder} = nothing
tr2bl::Maybe{TableCellBorder} = nothing
end
"""
ParagraphBorders(; kwargs...)
Holds properties for the borders of a [`Paragraph`](@ref) and is used by [`ParagraphProperties`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `top::`[`ParagraphBorder`](@ref) | The properties of the top border. |
| `bottom::`[`ParagraphBorder`](@ref) | The properties of the bottom border. |
| `left::`[`ParagraphBorder`](@ref) | The properties of the left border. |
| `right::`[`ParagraphBorder`](@ref) | The properties of the right border. |
| `between::`[`ParagraphBorder`](@ref) | The properties of horizontal border that lies between adjacent paragraphs. |
"""
Base.@kwdef struct ParagraphBorders
top::Maybe{ParagraphBorder} = nothing
left::Maybe{ParagraphBorder} = nothing
bottom::Maybe{ParagraphBorder} = nothing
right::Maybe{ParagraphBorder} = nothing
between::Maybe{ParagraphBorder} = nothing
end
"""
TableCellMargins(; kwargs...)
Holds properties for the margins of a [`TableCell`](@ref) and is used by [`TableCellProperties`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `top::`[`Twip`](@ref) | The top margin. |
| `bottom::`[`Twip`](@ref) | The bottom margin. |
| `start::`[`Twip`](@ref) | The left margin in left-to-right text. |
| `stop::`[`Twip`](@ref) | The right margin in left-to-right text. |
"""
Base.@kwdef struct TableCellMargins
top::Maybe{Twip} = nothing
start::Maybe{Twip} = nothing
bottom::Maybe{Twip} = nothing
stop::Maybe{Twip} = nothing
end
"""
TableCellMargins(; kwargs...)
Holds properties for the default margins of all [`TableCell`](@ref)s
in a [`Table`](@ref) and is used by [`TableProperties`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `top::`[`Twip`](@ref) | The top margin. |
| `bottom::`[`Twip`](@ref) | The bottom margin. |
| `start::`[`Twip`](@ref) | The left margin in left-to-right text. |
| `stop::`[`Twip`](@ref) | The right margin in left-to-right text. |
"""
Base.@kwdef struct TableLevelCellMargins
top::Maybe{Twip} = nothing
start::Maybe{Twip} = nothing
bottom::Maybe{Twip} = nothing
stop::Maybe{Twip} = nothing
end
"""
InlineDrawing{T}(; image::T, width::EMU, height::EMU)
Create an `InlineDrawing` object which, as the name implies, can be placed inline with
text inside [`Run`](@ref)s.
WriteDocx supports different types `T` for the `image` argument.
If `T` is a `String`, `image` is treated as the file path to a .png or .svg image.
You can also use [`SVGWithPNGFallback`](@ref) to place .svg images with better fallback behavior.
Width and height of the placed image are set via `width` and `height`, note that you have to
determine these values yourself for any image you place, a correct aspect ratio will not
be determined automatically.
"""
Base.@kwdef struct InlineDrawing{T}
image::T
width::EMU
height::EMU
end
is_inline_element(::Type{<:InlineDrawing}) = true
"""
SVGWithPNGFallback(; svg::String, png::String)
Create a `SVGWithPNGFallback` for the svg file at path `svg` and the fallback png
file at path `png`.
Word Online and other services like Slack preview don't work when a simple svg file is added via
[`InlineDrawing`](@ref)`{String}`.
`SVGWithPNGFallback` supplies a fallback png file which will be used for display in those situations.
Note that it is your responsibility to check whether the png file is an accurate
replacement for the svg.
"""
struct SVGWithPNGFallback
svg::String # path to SVG
png::String # path to PNG
function SVGWithPNGFallback(; svg::AbstractString, png::AbstractString)
svg = convert(String, svg)
png = convert(String, png)
if !endswith(svg, ".svg")
throw(ArgumentError("SVG file needs to end with .svg, got $svg"))
end
if !endswith(png, ".png")
throw(ArgumentError("PNG file needs to end with .png, got $png"))
end
new(svg, png)
end
end
InlineDrawing(image::T, width, height) where T = InlineDrawing{T}(image, convert(EMU, width), convert(EMU, height))
@partialkw struct Style{T}
id::String
properties::T
@kwargs
name::String
default::Bool = false
based_on::Maybe{String} = nothing
ui_priority::Maybe{Int} = nothing
next::Maybe{String} = nothing
link::Maybe{String} = nothing
custom_style::Maybe{Bool} = nothing
primary_style::Bool = true # important style (without this it doesn't show up in the Word user interface)
end
"""
RunProperties(; kwargs...)
Holds properties for a [`Run`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `style::String` | The name of the style applied to this `Run`. |
| `color::`[`AutomaticDefault`](@ref)`{HexColor}` | The color of the text. |
| `size::`[`HalfPoint`](@ref) | The font size. |
| `valign::`[`VerticalAlignment`](@ref)`.T` | Whether text is shown with baseline, superscript or subscript style. |
| `fonts::`[`Fonts`](@ref) | The font settings for this text. |
| `bold::Bool` | Whether text should be bold. Note that this works like a toggle when nested, turning boldness off again the second time it's `true`. |
| `italic::Bool` | Whether text should be italic. Note that this works like a toggle when nested, turning italic style off again the second time it's `true`. |
"""
Base.@kwdef struct RunProperties
style::Maybe{String} = nothing
color::Maybe{AutomaticDefault{HexColor}} = nothing
size::Maybe{HalfPoint} = nothing
valign::Maybe{VerticalAlignment.T} = nothing
fonts::Maybe{Fonts} = nothing
bold::Maybe{Bool} = nothing
italic::Maybe{Bool} = nothing
end
"""
Justification
An enum that can be either `start`, `stop`, `center`, `both` or `distribute`.
"""
@enumx Justification start stop center both distribute
Base.@kwdef struct Spacing
before::Maybe{Twip} = nothing
after::Maybe{Twip} = nothing
end
"""
ShadingPattern
An enum that can be either `clear`, `diag_cross`, `diag_stripe`, `horz_cross`, `horz_stripe`, `nil`, `thin_diag_cross`, or `solid`.
"""
@enumx ShadingPattern clear diag_cross diag_stripe horz_cross horz_stripe nil thin_diag_cross solid
Base.@kwdef struct Shading
pattern::ShadingPattern.T = ShadingPattern.clear
fill::AutomaticDefault{HexColor} = automatic
color::AutomaticDefault{HexColor} = automatic
end
"""
ParagraphProperties(; kwargs...)
Holds properties for a [`Paragraph`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `style::String` | The name of the style applied to this `Paragraph`. |
| `justification::`[`Justification`](@ref)`.T` | The justification of the paragraph. |
"""
Base.@kwdef struct ParagraphProperties
style::Maybe{String} = nothing
justification::Maybe{Justification.T} = nothing
keep_next::Maybe{Bool} = nothing
keep_lines::Maybe{Bool} = nothing
page_break_before::Maybe{Bool} = nothing
run_properties::Maybe{RunProperties} = nothing
spacing::Maybe{Spacing} = nothing
borders::Maybe{ParagraphBorders} = nothing
shading::Maybe{Shading} = nothing
end
"""
DocDefaults(; kwargs...)
Holds the default run and paragraph properties for a [`Document`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `run::`[`RunProperties`](@ref) | The default properties for every [`Run`](@ref). |
| `paragraph::`[`ParagraphProperties`](@ref) | The default properties for every [`Paragraph`](@ref). |
"""
Base.@kwdef struct DocDefaults
run::Maybe{RunProperties} = nothing
paragraph::Maybe{ParagraphProperties} = nothing
end
"""
Styles(styles::Vector{Style}, doc_defaults::DocDefaults)
Styles(styles; kwargs...)
Holds style information for a [`Document`](@ref).
The second convenience constructor forwards all keyword arguments to [`DocDefaults`](@ref).
"""
struct Styles
styles::Vector{Style}
doc_defaults::DocDefaults
function Styles(styles, doc_defaults)
style_ids = Set([style.id for style in styles])
styles = Vector{Style}(styles)
for style in default_styles()
if style.id ∉ style_ids
push!(styles, style)
end
end
new(styles, doc_defaults)
end
end
Styles(styles; kwargs...) = Styles(styles, DocDefaults(; kwargs...))
"""
Run(children::AbstractVector, properties::RunProperties)
Run(children::AbstractVector; kwargs...)
Create a `Run` with `children` who all have to satisfy `is_inline_element`.
The second convenience constructor forwards all keyword arguments to the
[`RunProperties`](@ref) constructor.
"""
struct Run
children::Vector{Any}
properties::RunProperties
function Run(children::AbstractVector, properties::RunProperties)
children = validate_elements(is_inline_element, children, :Run)
new(children, properties)
end
end
Run(children::AbstractVector; kwargs...) = Run(children, RunProperties(; kwargs...))
is_run_element(::Type{Run}) = true
"""
TableProperties(; kwargs...)
Holds properties for a [`Table`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `margins::TableLevelCellMargins` | Margins for all cells in the table. |
| `spacing::Twip` | The space between adjacent cells and the edges of the table. |
| `justification::`[`Justification`](@ref)`.T` | The justification of the table. |
"""
Base.@kwdef struct TableProperties
margins::Maybe{TableLevelCellMargins} = nothing
spacing::Maybe{Twip} = nothing
justification::Maybe{Justification.T} = nothing
end
@enumx HeightRule at_least exact auto
struct TableRowHeight
height::Twip
hrule::Maybe{HeightRule.T}
end
TableRowHeight(height; hrule = nothing) = TableRowHeight(height, hrule)
"""
TableRowProperties(; kwargs...)
Holds properties for a [`TableRow`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `header::Bool` | Whether this row should be part of the header section which is repeated after every page break. |
| `height::TableRowHeight` | The height of the table row. |
"""
Base.@kwdef struct TableRowProperties
header::Maybe{Bool} = nothing
height::Maybe{TableRowHeight} = nothing
end
"""
VerticalAlign
An enum that can be `bottom`, `center` or `top`.
"""
@enumx VerticalAlign bottom center top
"""
TableCellProperties(; kwargs...)
Holds properties for a [`TableCell`](@ref).
All properties are optional.
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `borders::TableCellBorders` | The border style of the cell. |
| `vertical_merge::Bool` | Should be set to `true` if this cell should be merged with the one above it. |
| `gridspan::Int` | The number of cells this cell should span in horizontal direction. |
| `margins::TableCellMargins` | The margins of the cell. |
| `valign::`[`VerticalAlign`](@ref)`.T` | The vertical alignment of the content in the cell. |
| `hide_mark::Bool` | If `true`, hides the editor mark so that the table cell can fully collapse if it's empty. |
"""
Base.@kwdef struct TableCellProperties
borders::Maybe{TableCellBorders} = nothing
vertical_merge::Maybe{Bool} = nothing
gridspan::Maybe{Int} = nothing
margins::Maybe{TableCellMargins} = nothing
valign::Maybe{VerticalAlign.T} = nothing
hide_mark::Maybe{Bool} = nothing # cells can only collapse vertically if this is true, for example to use a cell only for its border
end
struct SimpleField
instruction::String
cached_value::Maybe{Run}
dirty::Maybe{Bool}
end
SimpleField(instruction; dirty = true) = SimpleField(instruction, nothing, dirty)
is_run_element(::Type{SimpleField}) = true
function page_number_field()
SimpleField("PAGE"; dirty = nothing) # it seems that page numbers are always recalculated, but if dirty is set they cause a warning
end
"""
ComplexField(instruction::String; dirty = true)
Creates a complex field with a specific `instruction` that has an effect in the
viewer application. If `dirty === true`, the field will be reevaluated when opening the
docx file.
The `ComplexField` element must be paired with a following [`ComplexFieldEnd`](@ref). For some
purposes, other elements may appear between the two.
"""
struct ComplexField
instruction::String
dirty::Maybe{Bool}
end
ComplexField(instruction; dirty = true) = ComplexField(instruction, dirty)
is_run_element(::Type{ComplexField}) = true
"""
ComplexFieldEnd()
Every [`ComplexField`](@ref) element must be paired with this element.
"""
struct ComplexFieldEnd end
is_run_element(::Type{ComplexFieldEnd}) = true
is_block_element(x) = false
is_run_element(x) = false
"""
Paragraph(children::Vector{Any}, properties::ParagraphProperties)
Paragraph(children::AbstractVector; kwargs...)
A paragraph can contain children that satisfy `is_run_element`.
The second convenience constructor forwards all keyword arguments to [`ParagraphProperties`](@ref).
"""
struct Paragraph
children::Vector{Any}
properties::ParagraphProperties
function Paragraph(children::AbstractVector, properties::ParagraphProperties)
children = validate_elements(is_run_element, children, :Paragraph)
new(children, properties)
end
end
is_block_element(::Type{Paragraph}) = true
Paragraph(children; kwargs...) = Paragraph(children, ParagraphProperties(; kwargs...))
"""
TableCell(children::Vector{Any}, properties::TableCellProperties)
TableCell(children::AbstractVector; kwargs...)
One cell of a [`Table`](@ref) which can hold elements that satisfy `is_block_element`.
The second convenience constructor forwards all keyword arguments to [`TableCellProperties`](@ref).
"""
struct TableCell
children::Vector{Any}
properties::TableCellProperties
function TableCell(children::AbstractVector, properties::TableCellProperties)
children = validate_elements(is_block_element, children, :TableCell)
new(children, properties)
end
end
TableCell(children::AbstractVector; kwargs...) = TableCell(children, TableCellProperties(; kwargs...))
"""
TableRow(cells::Vector{TableCell}, properties::TableRowProperties)
TableRow(cells; kwargs...)
One row of a [`Table`](@ref) which can hold a vector of [`TableCell`](@ref)s.
The second convenience constructor forwards all keyword arguments to [`TableRowProperties`](@ref).
"""
struct TableRow
cells::Vector{TableCell}
properties::TableRowProperties
end
TableRow(cells; kwargs...) = TableRow(cells, TableRowProperties(; kwargs...))
"""
Table(rows::Vector{TableRow}, properties::TableProperties)
Table(rows; kwargs...)
A table which can hold a vector of [`TableRow`](@ref)s.
The second convenience constructor forwards all keyword arguments to [`TableProperties`](@ref).
"""
struct Table
rows::Vector{TableRow}
properties::TableProperties
end
is_block_element(::Type{Table}) = true
Table(rows; kwargs...) = Table(rows, TableProperties(; kwargs...))
@enumx PageOrientation landscape portrait
struct PageSize
width::Twip
height::Twip
orientation::PageOrientation.T
end
"""
PageSize(width, height)
The size of a page. If `width > height`, the page is set to `PageOrientation.landscape`.
"""
function PageSize(width, height)
orientation = height >= width ? PageOrientation.portrait : PageOrientation.landscape
PageSize(width, height, orientation)
end
@enumx PageVerticalAlign both bottom center top
function validate_elements(predicate, elements::AbstractVector, target::Symbol)
elements = convert(Vector{Any}, elements)
for element in elements
if !predicate(typeof(element))
error("Element of type $(typeof(element)) does not satisfy `$predicate` and cannot be placed in a `$target`.")
end
end
return elements
end
"""
Header(children::AbstractVector)
Contains elements for use in a [`Section`](@ref)'s header section.
Each element should satisfy `is_block_element`.
"""
struct Header
children::Vector{Any}
function Header(children::AbstractVector)
children = validate_elements(is_block_element, children, :Header)
new(children)
end
end
"""
Headers(; default::Header, [first::Header, even::Header])
Holds information about the [`Header`](@ref)s of a [`Section`](@ref).
A `default` `Header` must always be specified. If `first` is set, the
first page of the section gets this separate header. If `even` is
set, every even-numbered page of the section gets this separate header, making
`default` effectively mean `odd`.
"""
Base.@kwdef struct Headers
default::Header
first::Maybe{Header} = nothing
even::Maybe{Header} = nothing
end
"""
Footer(children::AbstractVector)
Contains elements for use in a [`Section`](@ref)'s footer section.
Each element should satisfy `is_block_element`.
"""
struct Footer
children::Vector{Any}
function Footer(children::AbstractVector)
children = validate_elements(is_block_element, children, :Footer)
new(children)
end
end
"""
Footers(; default::Footer, [first::Footer, even::Footer])
Holds information about the [`Footer`](@ref)s of a [`Section`](@ref).
A `default` `Footer` must always be specified. If `first` is set, the
first page of the section gets this separate footer. If `even` is
set, every even-numbered page of the section gets this separate footer, making
`default` effectively mean `odd`.
"""
Base.@kwdef struct Footers
default::Footer
first::Maybe{Footer} = nothing
even::Maybe{Footer} = nothing
end
"""
SectionProperties(; kwargs...)
Holds properties for a [`Section`](@ref).
## Keyword arguments
| Keyword | Description |
| :-- | :-- |
| `pagesize::PageSize` | The size of each page in the section. |
| `valign::PageVerticalAlign.T` | The vertical alignment of content on each page of the section. |
| `headers::`[`Headers`](@ref) | Defines the header content shown at the top of each page of the section. |
| `footers::`[`Footers`](@ref) | Defines the footer content shown at the bottom of each page of the section. |
"""
Base.@kwdef struct SectionProperties
pagesize::Maybe{PageSize} = nothing
valign::Maybe{PageVerticalAlign.T} = nothing
headers::Maybe{Headers} = nothing
footers::Maybe{Footers} = nothing
end
"""
Section(children::AbstractVector, properties::SectionProperties)