@@ -1,4 +1,4 @@
1−{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
1+{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, OverloadedStrings #-}
3Copyright (C) 2016-17 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2016-17 John MacFarlane <jgm@berkeley.edu>
@@ -32,6 +32,7 @@ and info messages.
32module Text.Pandoc.Logging (
32module Text.Pandoc.Logging (
33 Verbosity(..)
33 Verbosity(..)
34 , LogMessage(..)
34 , LogMessage(..)
35 , messageVerbosity
36 , messageVerbosity
@@ -39,18 +40,25 @@ import Text.Parsec.Pos
39import Data.Data (Data)
40import Data.Data (Data)
40import Data.Generics (Typeable)
41import Data.Generics (Typeable)
41import GHC.Generics (Generic)
42import GHC.Generics (Generic)
43+import qualified Data.Text as Text
45+import Text.Pandoc.Definition
43-- | Verbosity level.
47-- | Verbosity level.
44data Verbosity = ERROR | WARNING | INFO | DEBUG
48data Verbosity = ERROR | WARNING | INFO | DEBUG
45 deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
49 deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
51+instance ToJSON Verbosity where
52+ toJSON x = toJSON (show x)
47data LogMessage =
54data LogMessage =
48 SkippedInput String SourcePos
55 SkippedInput String SourcePos
50 | YamlSectionNotAnObject SourcePos
56 | YamlSectionNotAnObject SourcePos
51 | DuplicateLinkReference String SourcePos
57 | DuplicateLinkReference String SourcePos
52 | DuplicateNoteReference String SourcePos
58 | DuplicateNoteReference String SourcePos
53 | ParsingUnescaped String SourcePos
59 | ParsingUnescaped String SourcePos
60+ | InlineNotRendered Inline
61+ | BlockNotRendered Block
54 | DocxCommentWillNotRetainFormatting String
62 | DocxCommentWillNotRetainFormatting String
55 | CouldNotFetchResource String String
63 | CouldNotFetchResource String String
56 | CouldNotDetermineImageSize String
64 | CouldNotDetermineImageSize String
@@ -58,15 +66,100 @@ data LogMessage =
58 | CouldNotConvertTeXMath String
66 | CouldNotConvertTeXMath String
59 deriving (Show, Eq, Data, Ord, Typeable, Generic)
67 deriving (Show, Eq, Data, Ord, Typeable, Generic)
69+instance ToJSON LogMessage where
70+ toJSON x = object $ "verbosity" .= toJSON (messageVerbosity x) :
72+ SkippedInput s pos ->
73+ ["type" .= String "SkippedInput",
74+ "contents" .= Text.pack s,
75+ "source" .= Text.pack (sourceName pos),
76+ "line" .= sourceLine pos,
77+ "column" .= sourceColumn pos]
78+ YamlSectionNotAnObject pos ->
79+ ["type" .= String "YamlSectionNotAnObject",
80+ "source" .= Text.pack (sourceName pos),
81+ "line" .= toJSON (sourceLine pos),
82+ "column" .= toJSON (sourceColumn pos)]
83+ DuplicateLinkReference s pos ->
84+ ["type" .= String "DuplicateLinkReference",
85+ "contents" .= Text.pack s,
86+ "source" .= Text.pack (sourceName pos),
87+ "line" .= toJSON (sourceLine pos),
88+ "column" .= toJSON (sourceColumn pos)]
89+ DuplicateNoteReference s pos ->
90+ ["type" .= String "DuplicateNoteReference",
91+ "contents" .= Text.pack s,
92+ "source" .= Text.pack (sourceName pos),
93+ "line" .= toJSON (sourceLine pos),
94+ "column" .= toJSON (sourceColumn pos)]
95+ ParsingUnescaped s pos ->
96+ ["type" .= String "ParsingUnescaped",
97+ "contents" .= Text.pack s,
98+ "source" .= Text.pack (sourceName pos),
99+ "line" .= toJSON (sourceLine pos),
100+ "column" .= toJSON (sourceColumn pos)]
101+ InlineNotRendered il ->
102+ ["type" .= String "InlineNotRendered",
103+ "contents" .= toJSON il]
104+ BlockNotRendered bl ->
105+ ["type" .= String "BlockNotRendered",
106+ "contents" .= toJSON bl]
107+ DocxCommentWillNotRetainFormatting s ->
108+ ["type" .= String "DocxCommentWillNotRetainFormatting",
109+ "commentId" .= Text.pack s]
110+ CouldNotFetchResource fp s ->
111+ ["type" .= String "CouldNotFetchResource",
112+ "path" .= Text.pack fp,
113+ "message" .= Text.pack s]
114+ CouldNotDetermineImageSize fp ->
115+ ["type" .= String "CouldNotDetermineImageSize",
116+ "path" .= Text.pack fp]
117+ CouldNotDetermineMimeType fp ->
118+ ["type" .= String "CouldNotDetermineMimeType",
119+ "path" .= Text.pack fp]
120+ CouldNotConvertTeXMath s ->
121+ ["type" .= String "CouldNotConvertTeXMath",
122+ "contents" .= Text.pack s]
124+showLogMessage :: LogMessage -> String
127+ SkippedInput s pos ->
128+ "Skipped '" ++ s ++ "' at " ++ show pos
129+ YamlSectionNotAnObject pos ->
130+ "YAML metadata section is not an object at " ++ show pos
131+ DuplicateLinkReference s pos ->
132+ "Duplicate link reference '" ++ s ++ "' at " ++ show pos
133+ DuplicateNoteReference s pos ->
134+ "Duplicate note reference '" ++ s ++ "' at " ++ show pos
135+ ParsingUnescaped s pos ->
136+ "Parsing unescaped '" ++ s ++ "' at " ++ show pos
137+ InlineNotRendered il ->
138+ "Not rendering " ++ show il
139+ BlockNotRendered bl ->
140+ "Not rendering " ++ show bl
141+ DocxCommentWillNotRetainFormatting s ->
142+ "Docx comment with id '" ++ s ++ "' will not retain formatting"
143+ CouldNotFetchResource fp s ->
144+ "Could not fetch resource '" ++ fp ++ "'" ++
145+ if null s then "" else (": " ++ s)
146+ CouldNotDetermineImageSize fp ->
147+ "Could not determine image size for '" ++ fp ++ "'"
148+ CouldNotDetermineMimeType fp ->
149+ "Could not determine mime type for '" ++ fp ++ "'"
150+ CouldNotConvertTeXMath s ->
151+ "Could not convert TeX math '" ++ s ++ "', rendering as TeX"
61messageVerbosity:: LogMessage -> Verbosity
153messageVerbosity:: LogMessage -> Verbosity
62messageVerbosity msg =
154messageVerbosity msg =
63 case msg of
155 case msg of
64 SkippedInput{} -> INFO
156 SkippedInput{} -> INFO
65− NotRendered{} -> INFO
66 YamlSectionNotAnObject{} -> WARNING
157 YamlSectionNotAnObject{} -> WARNING
67 DuplicateLinkReference{} -> WARNING
158 DuplicateLinkReference{} -> WARNING
68 DuplicateNoteReference{} -> WARNING
159 DuplicateNoteReference{} -> WARNING
69 ParsingUnescaped{} -> INFO
160 ParsingUnescaped{} -> INFO
161+ InlineNotRendered{} -> INFO
162+ BlockNotRendered{} -> INFO
70 DocxCommentWillNotRetainFormatting{} -> INFO
163 DocxCommentWillNotRetainFormatting{} -> INFO
71 CouldNotFetchResource{} -> WARNING
164 CouldNotFetchResource{} -> WARNING
72 CouldNotDetermineImageSize{} -> WARNING
165 CouldNotDetermineImageSize{} -> WARNING