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
|
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Text.Pandoc.Logging
Copyright : Copyright (C) 2006-2019 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
This module provides data types and functions for warnings
and info messages.
-}
module Text.Pandoc.Logging (
Verbosity(..)
, LogMessage(..)
, encodeLogMessages
, showLogMessage
, messageVerbosity
) where
import Prelude
import Control.Monad (mzero)
import Data.Aeson
import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
keyOrder)
import qualified Data.ByteString.Lazy as BL
import Data.Data (Data, toConstr)
import Data.List (isSuffixOf, intercalate)
import qualified Data.Text as Text
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
import Text.Pandoc.Definition
import Text.Parsec.Pos
-- | Verbosity level.
data Verbosity = ERROR | WARNING | INFO
deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
instance ToJSON Verbosity where
toJSON x = toJSON (show x)
instance FromJSON Verbosity where
parseJSON (String t) =
case t of
"ERROR" -> return ERROR
"WARNING" -> return WARNING
"INFO" -> return INFO
_ -> mzero
parseJSON _ = mzero
data LogMessage =
SkippedContent String SourcePos
| IgnoredElement String
| CouldNotParseYamlMetadata String SourcePos
| DuplicateLinkReference String SourcePos
| DuplicateNoteReference String SourcePos
| NoteDefinedButNotUsed String SourcePos
| DuplicateIdentifier String SourcePos
| ReferenceNotFound String SourcePos
| CircularReference String SourcePos
| UndefinedToggle String SourcePos
| ParsingUnescaped String SourcePos
| CouldNotLoadIncludeFile String SourcePos
| MacroAlreadyDefined String SourcePos
| InlineNotRendered Inline
| BlockNotRendered Block
| DocxParserWarning String
| IgnoredIOError String
| CouldNotFetchResource String String
| CouldNotDetermineImageSize String String
| CouldNotConvertImage String String
| CouldNotDetermineMimeType String
| CouldNotConvertTeXMath String String
| CouldNotParseCSS String
| Fetching String
| Extracting String
| NoTitleElement String
| NoLangSpecified
| InvalidLang String
| CouldNotHighlight String
| MissingCharacter String
| Deprecated String String
| NoTranslation String
| CouldNotLoadTranslations String String
| UnusualConversion String
| UnexpectedXmlElement String String
| UnknownOrgExportOption String
| UnknownExtensions [String] String
deriving (Show, Eq, Data, Ord, Typeable, Generic)
instance ToJSON LogMessage where
toJSON x = object $
"verbosity" .= toJSON (messageVerbosity x) :
"type" .= toJSON (show $ toConstr x) :
case x of
SkippedContent s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= sourceLine pos,
"column" .= sourceColumn pos]
IgnoredElement s ->
["contents" .= Text.pack s]
CouldNotParseYamlMetadata s pos ->
["message" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
DuplicateLinkReference s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
NoteDefinedButNotUsed s pos ->
["key" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
DuplicateNoteReference s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
DuplicateIdentifier s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
ReferenceNotFound s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
CircularReference s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
UndefinedToggle s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
ParsingUnescaped s pos ->
["contents" .= Text.pack s,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
CouldNotLoadIncludeFile fp pos ->
["path" .= Text.pack fp,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
MacroAlreadyDefined name pos ->
["name" .= Text.pack name,
"source" .= Text.pack (sourceName pos),
"line" .= toJSON (sourceLine pos),
"column" .= toJSON (sourceColumn pos)]
InlineNotRendered il ->
["contents" .= toJSON il]
BlockNotRendered bl ->
["contents" .= toJSON bl]
DocxParserWarning s ->
["contents" .= Text.pack s]
IgnoredIOError s ->
["contents" .= Text.pack s]
CouldNotFetchResource fp s ->
["path" .= Text.pack fp,
"message" .= Text.pack s]
CouldNotDetermineImageSize fp s ->
["path" .= Text.pack fp,
"message" .= Text.pack s]
CouldNotConvertImage fp s ->
["path" .= Text.pack fp,
"message" .= Text.pack s]
CouldNotDetermineMimeType fp ->
["path" .= Text.pack fp]
CouldNotConvertTeXMath s msg ->
["contents" .= Text.pack s,
"message" .= Text.pack msg]
CouldNotParseCSS msg ->
["message" .= Text.pack msg]
Fetching fp ->
["path" .= Text.pack fp]
Extracting fp ->
["path" .= Text.pack fp]
NoTitleElement fallback ->
["fallback" .= Text.pack fallback]
NoLangSpecified -> []
InvalidLang s ->
["lang" .= Text.pack s]
CouldNotHighlight msg ->
["message" .= Text.pack msg]
MissingCharacter msg ->
["message" .= Text.pack msg]
Deprecated thing msg ->
["thing" .= Text.pack thing,
"message" .= Text.pack msg]
NoTranslation term ->
["term" .= Text.pack term]
CouldNotLoadTranslations lang msg ->
["lang" .= Text.pack lang,
"message" .= Text.pack msg]
UnusualConversion msg ->
["message" .= Text.pack msg]
UnexpectedXmlElement element parent ->
["element" .= Text.pack element,
"parent" .= Text.pack parent]
UnknownOrgExportOption option ->
["option" .= Text.pack option]
UnknownExtensions exts format ->
["extensions" .= map Text.pack exts
,"format" .= Text.pack format]
showPos :: SourcePos -> String
showPos pos = sn ++ "line " ++
show (sourceLine pos) ++ " column " ++ show (sourceColumn pos)
where sn = if sourceName pos == "source" || sourceName pos == ""
then ""
else sourceName pos ++ " "
encodeLogMessages :: [LogMessage] -> BL.ByteString
encodeLogMessages ms =
encodePretty' defConfig{ confCompare =
keyOrder [ "type", "verbosity", "contents", "message", "path",
"source", "line", "column" ] } ms
showLogMessage :: LogMessage -> String
showLogMessage msg =
case msg of
SkippedContent s pos ->
"Skipped '" ++ s ++ "' at " ++ showPos pos
IgnoredElement s ->
"Ignored element " ++ s
CouldNotParseYamlMetadata s pos ->
"Could not parse YAML metadata at " ++ showPos pos ++
if null s then "" else ": " ++ s
DuplicateLinkReference s pos ->
"Duplicate link reference '" ++ s ++ "' at " ++ showPos pos
DuplicateNoteReference s pos ->
"Duplicate note reference '" ++ s ++ "' at " ++ showPos pos
NoteDefinedButNotUsed s pos ->
"Note with key '" ++ s ++ "' defined at " ++ showPos pos ++
" but not used."
DuplicateIdentifier s pos ->
"Duplicate identifier '" ++ s ++ "' at " ++ showPos pos
ReferenceNotFound s pos ->
"Reference not found for '" ++ s ++ "' at " ++ showPos pos
CircularReference s pos ->
"Circular reference '" ++ s ++ "' at " ++ showPos pos
UndefinedToggle s pos ->
"Undefined toggle '" ++ s ++ "' at " ++ showPos pos
ParsingUnescaped s pos ->
"Parsing unescaped '" ++ s ++ "' at " ++ showPos pos
CouldNotLoadIncludeFile fp pos ->
"Could not load include file '" ++ fp ++ "' at " ++ showPos pos
MacroAlreadyDefined name pos ->
"Macro '" ++ name ++ "' already defined, ignoring at " ++ showPos pos
InlineNotRendered il ->
"Not rendering " ++ show il
BlockNotRendered bl ->
"Not rendering " ++ show bl
DocxParserWarning s ->
"Docx parser warning: " ++ s
IgnoredIOError s ->
"IO Error (ignored): " ++ s
CouldNotFetchResource fp s ->
"Could not fetch resource '" ++ fp ++ "'" ++
if null s then "" else ": " ++ s
CouldNotDetermineImageSize fp s ->
"Could not determine image size for '" ++ fp ++ "'" ++
if null s then "" else ": " ++ s
CouldNotConvertImage fp s ->
"Could not convert image '" ++ fp ++ "'" ++
if null s then "" else ": " ++ s
CouldNotDetermineMimeType fp ->
"Could not determine mime type for '" ++ fp ++ "'"
CouldNotConvertTeXMath s m ->
"Could not convert TeX math '" ++ s ++ "', rendering as TeX" ++
if null m then "" else ':' : '\n' : m
CouldNotParseCSS m ->
"Could not parse CSS" ++ if null m then "" else ':' : '\n' : m
Fetching fp ->
"Fetching " ++ fp ++ "..."
Extracting fp ->
"Extracting " ++ fp ++ "..."
NoTitleElement fallback ->
"This document format requires a nonempty <title> element.\n" ++
"Please specify either 'title' or 'pagetitle' in the metadata,\n" ++
"e.g. by using --metadata pagetitle=\"...\" on the command line.\n" ++
"Falling back to '" ++ fallback ++ "'"
NoLangSpecified ->
"No value for 'lang' was specified in the metadata.\n" ++
"It is recommended that lang be specified for this format."
InvalidLang s ->
"Invalid 'lang' value '" ++ s ++ "'.\n" ++
"Use an IETF language tag like 'en-US'."
CouldNotHighlight m ->
"Could not highlight code block:\n" ++ m
MissingCharacter m ->
"Missing character: " ++ m
Deprecated t m ->
"Deprecated: " ++ t ++
if null m
then ""
else ". " ++ m
NoTranslation t ->
"The term " ++ t ++ " has no translation defined."
CouldNotLoadTranslations lang m ->
"Could not load translations for " ++ lang ++
if null m then "" else '\n' : m
UnusualConversion m ->
"Unusual conversion: " ++ m
UnexpectedXmlElement element parent ->
"Unexpected XML element " ++ element ++ " in " ++ parent
UnknownOrgExportOption option ->
"Ignoring unknown Org export option: " ++ option
UnknownExtensions exts format ->
"Could not deduce format from file extension " ++
intercalate " or " exts ++ "\n" ++
"Defaulting to " ++ format
messageVerbosity:: LogMessage -> Verbosity
messageVerbosity msg =
case msg of
SkippedContent{} -> INFO
IgnoredElement{} -> INFO
CouldNotParseYamlMetadata{} -> WARNING
DuplicateLinkReference{} -> WARNING
DuplicateNoteReference{} -> WARNING
NoteDefinedButNotUsed{} -> WARNING
DuplicateIdentifier{} -> WARNING
ReferenceNotFound{} -> WARNING
CircularReference{} -> WARNING
UndefinedToggle{} -> WARNING
CouldNotLoadIncludeFile f _
| ".sty" `isSuffixOf` f -> INFO
| otherwise -> WARNING
MacroAlreadyDefined{} -> WARNING
ParsingUnescaped{} -> INFO
InlineNotRendered{} -> INFO
BlockNotRendered{} -> INFO
DocxParserWarning{} -> INFO
IgnoredIOError{} -> WARNING
CouldNotFetchResource{} -> WARNING
CouldNotDetermineImageSize{} -> WARNING
CouldNotConvertImage{} -> WARNING
CouldNotDetermineMimeType{} -> WARNING
CouldNotConvertTeXMath{} -> WARNING
CouldNotParseCSS{} -> WARNING
Fetching{} -> INFO
Extracting{} -> INFO
NoTitleElement{} -> WARNING
NoLangSpecified -> INFO
InvalidLang{} -> WARNING
CouldNotHighlight{} -> WARNING
MissingCharacter{} -> WARNING
Deprecated{} -> WARNING
NoTranslation{} -> WARNING
CouldNotLoadTranslations{} -> WARNING
UnusualConversion {} -> WARNING
UnexpectedXmlElement {} -> WARNING
UnknownOrgExportOption {} -> WARNING
UnknownExtensions{} -> WARNING
|