Copyright | (c) 2019-2020 Vaclav Svejcar |
---|---|
License | BSD-3-Clause |
Maintainer | vaclav.svejcar@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
This module is the heart of Headroom as it contains functions for working with the license headers and the source code files.
Synopsis
- extractFileInfo :: FileType -> CtHeaderConfig -> Maybe TemplateMeta -> Text -> FileInfo
- addHeader :: FileInfo -> Text -> Text -> Text
- dropHeader :: FileInfo -> Text -> Text
- replaceHeader :: FileInfo -> Text -> Text -> Text
- findHeader :: CtHeaderConfig -> Text -> Maybe (Int, Int)
- findBlockHeader :: Text -> Text -> [Text] -> Int -> Maybe (Int, Int)
- findLineHeader :: Text -> [Text] -> Int -> Maybe (Int, Int)
- firstMatching :: [Regex] -> [Text] -> Maybe Int
- lastMatching :: [Regex] -> [Text] -> Maybe Int
- splitInput :: [Regex] -> [Regex] -> Text -> ([Text], [Text], [Text])
File info extraction
:: FileType | type of the detected file |
-> CtHeaderConfig | license header configuration |
-> Maybe TemplateMeta | metadata extracted from template |
-> Text | text used for detection |
-> FileInfo | resulting file info |
Extracts info about the processed file to be later used by the header detection/manipulation functions.
License header manipulation
:: FileInfo | info about file where header is added |
-> Text | text of the new header |
-> Text | text of the file where to add the header |
-> Text | resulting text with added header |
Adds given header at position specified by the FileInfo
. Does nothing if
any header is already present, use replaceHeader
if you need to
override it.
:: FileInfo | info about the file from which the header will be dropped |
-> Text | text of the file from which to drop the header |
-> Text | resulting text with dropped header |
Drops header at position specified by the FileInfo
from the given text.
Does nothing if no header is present.
:: FileInfo | info about the file in which to replace the header |
-> Text | text of the new header |
-> Text | text of the file where to replace the header |
-> Text | resulting text with replaced header |
Replaces existing header at position specified by the FileInfo
in the
given text. Basically combines addHeader
with dropHeader
. If no header
is present, then the given one is added to the text.
License header detection
:: CtHeaderConfig | appropriate header configuration |
-> Text | text in which to detect the header |
-> Maybe (Int, Int) | header position |
Finds header position in given text, where position is represented by
line number of first and last line of the header (numbered from zero).
Based on the HeaderSyntax
specified in given HeaderConfig
, this function
delegates its work to either findBlockHeader
or findLineHeader
.
>>>
:set -XFlexibleContexts
>>>
:set -XTypeFamilies
>>>
let hc = HeaderConfig ["hs"] 0 0 [] [] (BlockComment "{-" "-}")
>>>
findHeader hc "foo\nbar\n{- HEADER -}\nbaz"
Just (2,2)
:: Text | starting pattern (e.g. |
-> Text | ending pattern (e.g. |
-> [Text] | lines of text in which to detect the header |
-> Int | line number offset (adds to resulting position) |
-> Maybe (Int, Int) | header position |
Finds header in the form of multi-line comment syntax, which is delimited with starting and ending pattern.
>>>
findBlockHeader "{-" "-}" ["", "{- HEADER -}", "", ""] 0
Just (1,1)
:: Text | prefix pattern (e.g. |
-> [Text] | lines of text in which to detect the header |
-> Int | line number offset (adds to resulting position) |
-> Maybe (Int, Int) | header position |
Finds header in the form of single-line comment syntax, which is delimited with the prefix pattern.
>>>
findLineHeader "--" ["", "a", "-- first", "-- second", "foo"] 0
Just (2,3)
Finds very first line that matches the given regex (numbered from zero).
>>>
import Headroom.Data.Regex (re)
>>>
:set -XQuasiQuotes
>>>
firstMatching [[re|^foo|]] ["some text", "foo bar", "foo baz", "last"]
Just 1
Finds very last line that matches the given regex (numbered from zero).
>>>
import Headroom.Data.Regex (re)
>>>
:set -XQuasiQuotes
>>>
lastMatching [[re|^foo|]] ["some text", "foo bar", "foo baz", "last"]
Just 2
:: [Regex] | patterns for first split |
-> [Regex] | patterns for second split |
-> Text | text to split |
-> ([Text], [Text], [Text]) | result lines as |
Splits input lines into three parts:
- list of all lines located before the very last occurence of one of the conditions from the first condition list
- list of all lines between the first and last lists
- list of all lines located after the very first occurence of one of the conditions from the second condition list
If both first and second patterns are empty, then all lines are returned in the middle list.
>>>
import Headroom.Data.Regex (re)
>>>
:set -XQuasiQuotes
>>>
splitInput [[re|->|]] [[re|<-|]] "text\n->\nRESULT\n<-\nfoo"
(["text","->"],["RESULT"],["<-","foo"])
>>>
splitInput [] [[re|<-|]] "text\n->\nRESULT\n<-\nfoo"
([],["text","->","RESULT"],["<-","foo"])
>>>
splitInput [] [] "one\ntwo"
([],["one","two"],[])