File size: 3,543 Bytes
d94b56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
#
#   pyhwp : hwp file format parser in python
#   Copyright (C) 2010-2023 mete0r <https://github.com/mete0r>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from functools import partial
import logging

from ..utils import make_open_dest_file
from ..utils import wrap_open_dest_for_tty
from ..utils import wrap_open_dest
from ..utils import pager
from ..utils import syntaxhighlight
from ..utils import xmllint
from ..xmldump_flat import xmldump_flat
from ..xmlmodel import Hwp5File


logger = logging.getLogger(__name__)


def xmldump_nested(hwp5file, output, embedbin=False, xml_declaration=True):
    dump = hwp5file.xmlevents(embedbin=embedbin).dump
    dump = partial(dump, xml_declaration=xml_declaration)
    dump(output)


def main(args):
    ''' Transform <hwp5file> into an XML.
    '''

    fmt = args.format or 'nested'
    if fmt == 'flat':
        xmldump = partial(
            xmldump_flat,
            xml_declaration=not args.no_xml_decl
        )
    elif fmt == 'nested':
        xmldump = partial(
            xmldump_nested,
            xml_declaration=not args.no_xml_decl,
            embedbin=args.embedbin,
        )

    open_dest = make_open_dest_file(args.output)
    open_dest = wrap_open_dest_for_tty(open_dest, [
        pager(),
        syntaxhighlight('application/xml'),
    ] + ([
        xmllint(format=True),
    ] if not args.no_validate_wellformed else []))
    open_dest = wrap_open_dest(open_dest, [
        xmllint(encode='utf-8'),
        xmllint(c14n=True),
    ] if not args.no_validate_wellformed else [])

    hwp5file = Hwp5File(args.hwp5file)
    with open_dest() as output:
        xmldump(hwp5file, output)


def xml_argparser(subparsers, _):
    parser = subparsers.add_parser(
        'xml',
        help=_(
           'Transform .hwp files into an XML.'
        ),
        description=_(
           'Transform <hwp5file> into an XML.'
        ),
    )
    parser.add_argument(
        'hwp5file',
        metavar='<hwp5file>',
        help=_('.hwp file to analyze'),
    )
    parser.add_argument(
        '--embedbin',
        action='store_true',
        help=_('Embed BinData/* streams in the output XML.'),
    )
    parser.add_argument(
        '--no-xml-decl',
        action='store_true',
        help=_('Do not output <?xml ... ?> XML declaration.'),
    )
    parser.add_argument(
       '--output',
       metavar='<file>',
       help=_('Output filename.'),
    )
    parser.add_argument(
       '--format',
       metavar='<format>',
       help=_('"flat", "nested" (default: "nested")'),
    )
    parser.add_argument(
       '--no-validate-wellformed',
       action='store_true',
       help=_('Do not validate well-formedness of output.'),
    )
    parser.set_defaults(func=main)
    return parser