" &
"This module was loaded with LibPQ: https://github.com/sio/LibPQ" &
"
" &
"Module source code: " &
path &
"
",
/* Load text content from local file or from web */
Read.Text = (destination as text, optional local as logical) =>
let
Local = if local is null then true else local,
Fetcher = if Local then File.Contents else Web.Contents
in
Text.FromBinary(
Binary.Buffer(
try
Fetcher(destination)
otherwise
error Error.Record(
ERR_SOURCE_UNREADABLE,
"Read.Text: can not fetch from destination",
destination
)
)
),
/*
Read the first multiline comment from the source code in Power Query
Formula language (also known as M language). That comment is considered a
docstring for LibPQ
*/
Read.Docstring = (source_code as text) =>
let
Docstring = [
start = "/*",
end = "*/"
],
DocstringDirty = Text.BeforeDelimiter(
source_code,
Docstring[end]
),
BeforeDocstring = Text.BeforeDelimiter(
DocstringDirty,
Docstring[start]
),
MustBeEmpty = Text.Trim(BeforeDocstring),
DocstringText =
if
Text.Length(MustBeEmpty) = 0
then
Text.Trim(DocstringDirty, {"*", "/", " ", "#(cr)", "#(lf)", "#(tab)"})
else
""
in
DocstringText,
/*
Load Power Query function or module from file,
return null if destination unreadable
*/
Module.FromPath = (path as text, optional local as logical, optional name as text) =>
let
SourceCode = try Read.Text(path, local)
otherwise "null",
LoadedObject = Expression.Evaluate(SourceCode, #shared),
LoadTry = try LoadedObject,
CustomError = Record.TransformFields(
LoadTry[Error],
{"Detail", each CustomErrorDetail}
),
CustomErrorDetail = [
Original = LoadTry[Error][Detail],
LibPQ = ExtraMetadata
],
ExtraMetadata = [
LibPQ.Module = name,
LibPQ.Source = path,
LibPQ.Docstring = Read.Docstring(SourceCode),
Documentation.Name = LibPQ.Module,
Documentation.Description =
Text.Replace(LibPQ.Docstring, "#(lf)", "