function getTopicHelpText(hp, justH1)
    if nargin < 2
        justH1 = false;
    end
    if hp.isOperator
        hasLocalFunction = false;
    else
        [hp.topic, hasLocalFunction, shouldLink, ~, ~, helpFunction] = matlab.internal.language.introspective.fixLocalFunctionCase(hp.topic);

        if hasLocalFunction && shouldLink
            hp.fullTopic = hp.topic;
        else
            if hp.topic == "NaN" || hp.topic == "Inf"
                hp.topic = lower(hp.topic);
            end
            nameResolver = matlab.internal.language.introspective.resolveName(hp.topic, '', hp.justChecking, hp.wsVariables, hp.fixTypos);
            
            if nameResolver.isResolved && ~nameResolver.isCaseSensitive && hp.commandIsHelp && ~hp.fixTypos
                return;
            end
            
            classInfo          = nameResolver.classInfo;
            hp.fullTopic       = nameResolver.whichTopic;
            hp.topic           = nameResolver.resolvedTopic;
            hp.elementKeyword  = nameResolver.elementKeyword;
            hp.isInaccessible  = nameResolver.isInaccessible;
            hp.isBuiltin       = nameResolver.isBuiltin;
            hp.imports         = nameResolver.imports;
            hp.callingFunction = nameResolver.callingFunction;
            
            if nameResolver.foundVar
                hp.inputTopic = nameResolver.topicInput; % may be case corrected var
                hp.helpOnInstance = true;
                hp.displayBanner = true;
            else
                hp.isTypo = nameResolver.isTypo ||(nameResolver.isResolved && ~nameResolver.isCaseSensitive);            
            end
                        
            hp.displayBanner = hp.displayBanner || nameResolver.isUnderqualified;

            if ~isempty(classInfo)
                hp.extractFromClassInfo(classInfo);
            end
            
            if hp.getHelpTextFromDoc(classInfo, false)
                return;
            end

            if isempty(classInfo)
                [hp.topic, ~, hp.fullTopic, helpFunction] = matlab.internal.language.introspective.fixFileNameCase(hp.topic, '', hp.fullTopic, hp.callingFunction);
                
                if nameResolver.malformed
                   return; 
                end
            else
                hp.getHelpFromClassInfo(classInfo, justH1);
                getAlternateSourcedHelp(hp, classInfo, justH1);
                return;
            end
        end
        if ~hp.isInaccessible && ~isempty(helpFunction)
            hp.helpStr = matlab.internal.language.introspective.callHelpFunction(helpFunction, hp.fullTopic, justH1);
            hp.needsHotlinking = true;
            [~, hp.topic, fileExt] = fileparts(hp.fullTopic);
            split = regexp(fileExt, filemarker, 'split', 'once');
            if ~isscalar(split)
                hp.topic = [hp.topic, filemarker, split{2}];
            end
        end
    end

    getAlternateSourcedHelp(hp, [], justH1);

    if ~isempty(hp.helpStr) && ~hasLocalFunction
        dirInfos = matlab.internal.language.introspective.hashedDirInfo(hp.inputTopic)';
        if isempty(hp.fullTopic)
            for dirInfo = dirInfos
                hp.fullTopic = matlab.internal.language.introspective.extractCaseCorrectedName(dirInfo.path, hp.topic);
                if ~isempty(hp.fullTopic)
                    hp.topic = matlab.internal.language.introspective.minimizePath(hp.fullTopic, true);
                    hp.isDir = true;
                    return;
                end
            end
        elseif ~isempty(hp.objectSystemName)
            hp.topic = hp.objectSystemName;
        else
            [~, hp.topic] = hp.getPathItem;
            if hp.topic == "handle"
                hp.isMCOSClassOrConstructor = true;
            elseif hp.topic == "nan"
                hp.topic = 'NaN';
            elseif hp.topic == "inf"
                hp.topic = 'Inf';
            elseif ~hp.isDir
                hp.isDir = ~isempty(dirInfos);
            end
        end
    end
end

function getAlternateSourcedHelp(hp, classInfo, justH1)
    if ((isempty(hp.fullTopic) || hp.isDir) && isempty(hp.helpStr)) || ~isempty(regexp(hp.inputTopic, '^\w*$', 'once'))
        hp.getFolderHelp(justH1);
    end
    
    if isempty(hp.helpStr)
        hp.getHelpTextFromDoc(classInfo, true);
    end
    
    if isempty(hp.helpStr)
        hp.getBuiltinHelp(justH1);
    end
    
    if isempty(hp.helpStr) && ~hp.isInaccessible
        hp.getDefaultHelpFromSource();
    end
end

%   Copyright 2007-2020 The MathWorks, Inc.
