git.iko.soy/typeable/pandoc
on 34412cf help sign in
267 lines 10 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-
5Copyright (C) 2016-17 John MacFarlane <jgm@berkeley.edu>
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20-}
21{- |
22 Module : Text.Pandoc.Logging
23 Copyright : Copyright (C) 2006-2016 John MacFarlane
24 License : GNU GPL, version 2 or above
26 Maintainer : John MacFarlane <jgm@berkeley.edu>
27 Stability : alpha
28 Portability : portable
30This module provides data types and functions for warnings
31and info messages.
33-}
34module Text.Pandoc.Logging (
35 Verbosity(..)
36 , LogMessage(..)
37 , encodeLogMessages
38 , showLogMessage
39 , messageVerbosity
40 ) where
42import Data.Aeson
43import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
44 keyOrder)
45import qualified Data.ByteString.Lazy as BL
46import Data.Data (Data, toConstr)
47import Data.Generics (Typeable)
48import qualified Data.Text as Text
49import GHC.Generics (Generic)
50import Text.Pandoc.Definition
51import Text.Parsec.Pos
53-- | Verbosity level.
54data Verbosity = ERROR | WARNING | INFO | DEBUG
55 deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
57instance ToJSON Verbosity where
58 toJSON x = toJSON (show x)
60data LogMessage =
61 SkippedContent String SourcePos
62 | CouldNotParseYamlMetadata String SourcePos
63 | DuplicateLinkReference String SourcePos
64 | DuplicateNoteReference String SourcePos
65 | DuplicateIdentifier String SourcePos
66 | ReferenceNotFound String SourcePos
67 | CircularReference String SourcePos
68 | ParsingUnescaped String SourcePos
69 | CouldNotLoadIncludeFile String SourcePos
70 | ParsingTrace String SourcePos
71 | InlineNotRendered Inline
72 | BlockNotRendered Block
73 | DocxParserWarning String
74 | CouldNotFetchResource String String
75 | CouldNotDetermineImageSize String String
76 | CouldNotConvertImage String String
77 | CouldNotDetermineMimeType String
78 | CouldNotConvertTeXMath String String
79 | CouldNotParseCSS String
80 | Fetching String
81 | NoTitleElement String
82 | NoLangSpecified
83 | CouldNotHighlight String
84 deriving (Show, Eq, Data, Ord, Typeable, Generic)
86instance ToJSON LogMessage where
87 toJSON x = object $
88 "verbosity" .= toJSON (messageVerbosity x) :
89 "type" .= toJSON (show $ toConstr x) :
90 case x of
91 SkippedContent s pos ->
92 ["contents" .= Text.pack s,
93 "source" .= Text.pack (sourceName pos),
94 "line" .= sourceLine pos,
95 "column" .= sourceColumn pos]
96 CouldNotParseYamlMetadata s pos ->
97 ["message" .= Text.pack s,
98 "source" .= Text.pack (sourceName pos),
99 "line" .= toJSON (sourceLine pos),
100 "column" .= toJSON (sourceColumn pos)]
101 DuplicateLinkReference s pos ->
102 ["contents" .= Text.pack s,
103 "source" .= Text.pack (sourceName pos),
104 "line" .= toJSON (sourceLine pos),
105 "column" .= toJSON (sourceColumn pos)]
106 DuplicateNoteReference s pos ->
107 ["contents" .= Text.pack s,
108 "source" .= Text.pack (sourceName pos),
109 "line" .= toJSON (sourceLine pos),
110 "column" .= toJSON (sourceColumn pos)]
111 DuplicateIdentifier s pos ->
112 ["contents" .= Text.pack s,
113 "source" .= Text.pack (sourceName pos),
114 "line" .= toJSON (sourceLine pos),
115 "column" .= toJSON (sourceColumn pos)]
116 ReferenceNotFound s pos ->
117 ["contents" .= Text.pack s,
118 "source" .= Text.pack (sourceName pos),
119 "line" .= toJSON (sourceLine pos),
120 "column" .= toJSON (sourceColumn pos)]
121 CircularReference s pos ->
122 ["contents" .= Text.pack s,
123 "source" .= Text.pack (sourceName pos),
124 "line" .= toJSON (sourceLine pos),
125 "column" .= toJSON (sourceColumn pos)]
126 ParsingUnescaped s pos ->
127 ["contents" .= Text.pack s,
128 "source" .= Text.pack (sourceName pos),
129 "line" .= toJSON (sourceLine pos),
130 "column" .= toJSON (sourceColumn pos)]
131 CouldNotLoadIncludeFile fp pos ->
132 ["path" .= Text.pack fp,
133 "source" .= Text.pack (sourceName pos),
134 "line" .= toJSON (sourceLine pos),
135 "column" .= toJSON (sourceColumn pos)]
136 ParsingTrace s pos ->
137 ["contents" .= Text.pack s,
138 "source" .= Text.pack (sourceName pos),
139 "line" .= sourceLine pos,
140 "column" .= sourceColumn pos]
141 InlineNotRendered il ->
142 ["contents" .= toJSON il]
143 BlockNotRendered bl ->
144 ["contents" .= toJSON bl]
145 DocxParserWarning s ->
146 ["contents" .= Text.pack s]
147 CouldNotFetchResource fp s ->
148 ["path" .= Text.pack fp,
149 "message" .= Text.pack s]
150 CouldNotDetermineImageSize fp s ->
151 ["path" .= Text.pack fp,
152 "message" .= Text.pack s]
153 CouldNotConvertImage fp s ->
154 ["path" .= Text.pack fp,
155 "message" .= Text.pack s]
156 CouldNotDetermineMimeType fp ->
157 ["path" .= Text.pack fp]
158 CouldNotConvertTeXMath s msg ->
159 ["contents" .= Text.pack s,
160 "message" .= Text.pack msg]
161 CouldNotParseCSS msg ->
162 ["message" .= Text.pack msg]
163 Fetching fp ->
164 ["path" .= Text.pack fp]
165 NoTitleElement fallback ->
166 ["fallback" .= Text.pack fallback]
167 NoLangSpecified -> []
168 CouldNotHighlight msg ->
169 ["message" .= Text.pack msg]
171showPos :: SourcePos -> String
172showPos pos = sn ++ "line " ++
173 show (sourceLine pos) ++ " column " ++ show (sourceColumn pos)
174 where sn = if sourceName pos == "source" || sourceName pos == ""
175 then ""
176 else sourceName pos ++ " "
178encodeLogMessages :: [LogMessage] -> BL.ByteString
179encodeLogMessages ms =
180 encodePretty' defConfig{ confCompare =
181 keyOrder [ "type", "verbosity", "contents", "message", "path",
182 "source", "line", "column" ] } ms
184showLogMessage :: LogMessage -> String
185showLogMessage msg =
186 case msg of
187 SkippedContent s pos ->
188 "Skipped '" ++ s ++ "' at " ++ showPos pos
189 CouldNotParseYamlMetadata s pos ->
190 "Could not parse YAML metadata at " ++ showPos pos ++
191 if null s then "" else (": " ++ s)
192 DuplicateLinkReference s pos ->
193 "Duplicate link reference '" ++ s ++ "' at " ++ showPos pos
194 DuplicateNoteReference s pos ->
195 "Duplicate note reference '" ++ s ++ "' at " ++ showPos pos
196 DuplicateIdentifier s pos ->
197 "Duplicate identifier '" ++ s ++ "' at " ++ showPos pos
198 ReferenceNotFound s pos ->
199 "Reference not found for '" ++ s ++ "' at " ++ showPos pos
200 CircularReference s pos ->
201 "Circular reference '" ++ s ++ "' at " ++ showPos pos
202 ParsingUnescaped s pos ->
203 "Parsing unescaped '" ++ s ++ "' at " ++ showPos pos
204 CouldNotLoadIncludeFile fp pos ->
205 "Could not load include file '" ++ fp ++ "' at " ++ showPos pos
206 ParsingTrace s pos ->
207 "Parsing trace at " ++ showPos pos ++ ": " ++ s
208 InlineNotRendered il ->
209 "Not rendering " ++ show il
210 BlockNotRendered bl ->
211 "Not rendering " ++ show bl
212 DocxParserWarning s ->
213 "Docx parser warning: " ++ s
214 CouldNotFetchResource fp s ->
215 "Could not fetch resource '" ++ fp ++ "'" ++
216 if null s then "" else (": " ++ s)
217 CouldNotDetermineImageSize fp s ->
218 "Could not determine image size for '" ++ fp ++ "'" ++
219 if null s then "" else (": " ++ s)
220 CouldNotConvertImage fp s ->
221 "Could not convert image '" ++ fp ++ "'" ++
222 if null s then "" else (": " ++ s)
223 CouldNotDetermineMimeType fp ->
224 "Could not determine mime type for '" ++ fp ++ "'"
225 CouldNotConvertTeXMath s m ->
226 "Could not convert TeX math '" ++ s ++ "', rendering as TeX" ++
227 if null m then "" else (':':'\n':m)
228 CouldNotParseCSS m ->
229 "Could not parse CSS" ++ if null m then "" else (':':'\n':m)
230 Fetching fp ->
231 "Fetching " ++ fp ++ "..."
232 NoTitleElement fallback ->
233 "This document format requires a nonempty <title> element.\n" ++
234 "Please specify either 'title' or 'pagetitle' in the metadata.\n" ++
235 "Falling back to '" ++ fallback ++ "'"
236 NoLangSpecified ->
237 "No value for 'lang' was specified in the metadata.\n" ++
238 "It is recommended that lang be specified for this format."
239 CouldNotHighlight m ->
240 "Could not highlight code block:\n" ++ m
242messageVerbosity:: LogMessage -> Verbosity
243messageVerbosity msg =
244 case msg of
245 SkippedContent{} -> INFO
246 CouldNotParseYamlMetadata{} -> WARNING
247 DuplicateLinkReference{} -> WARNING
248 DuplicateNoteReference{} -> WARNING
249 DuplicateIdentifier{} -> WARNING
250 ReferenceNotFound{} -> WARNING
251 CircularReference{} -> WARNING
252 CouldNotLoadIncludeFile{} -> WARNING
253 ParsingUnescaped{} -> INFO
254 ParsingTrace{} -> DEBUG
255 InlineNotRendered{} -> INFO
256 BlockNotRendered{} -> INFO
257 DocxParserWarning{} -> WARNING
258 CouldNotFetchResource{} -> WARNING
259 CouldNotDetermineImageSize{} -> WARNING
260 CouldNotConvertImage{} -> WARNING
261 CouldNotDetermineMimeType{} -> WARNING
262 CouldNotConvertTeXMath{} -> WARNING
263 CouldNotParseCSS{} -> WARNING
264 Fetching{} -> INFO
265 NoTitleElement{} -> WARNING
266 NoLangSpecified -> INFO
267 CouldNotHighlight{} -> WARNING