Skip to content

Instantly share code, notes, and snippets.

@Jakemangan
Created May 31, 2018 20:55
Show Gist options
  • Save Jakemangan/a5b85cafe3d37c70840b1f4c82bab0a1 to your computer and use it in GitHub Desktop.
Save Jakemangan/a5b85cafe3d37c70840b1f4c82bab0a1 to your computer and use it in GitHub Desktop.
class Challenge {
private static String hashes;
private static String content;
private static int numHashes;
/*
* Parses input markdown into HTML header format
* @Param String markdown - String holding markdown string for converstion
* @Return String output - HTML format of input markdown
*/
public static String markdownParser( String markdown )
{
//Initalise values
hashes = "";
content = "";
numHashes = 0;
System.out.println("Input markdown: '" + markdown + "'");
//Trim markdown to remove leading spaces
markdown = markdown.trim();
//Check markdown input is valid before proceeding
if(checkInputValid(markdown))
{
System.out.println("Input valid");
//Split string into hashes and content
splitString(markdown);
String output = constructOutput();
System.out.println("\n");
return output;
}
else
{
System.out.println("Input not valid\n");
return markdown;
}
}
/*
* Checks to ensure markdown input is valid
* Checks for:
* - First character in string is #
* - String contains " "
* - Number of hashes does not exceed 6
*
* @Param String input - String holding input markdown to be checked
* @Return boolean inputValid
*/
public static boolean checkInputValid(String input)
{
boolean inputValid = true;
//Get value of first character in string
String firstChar = input.substring(0, 1);
System.out.println("First char in string " + input + ": " + firstChar);
//Checks for presence of # and whitespace within input
if(firstChar.matches("#") && input.contains(" "))
{
inputValid = true;
}
else if(!firstChar.matches("#")) //If # is not present, input is invalid
{
System.out.println("Input markdown does not contain '#' as first character");
inputValid = false;
}
else //If whitespace is not present, input is invalid
{
System.out.println("Input markdown does not contain ' ' within string");
inputValid = false;
}
//Count number of hashes present in input string
int countHashes = input.length() - input.replace("#", "").length();
if(countHashes > 6)
{
System.out.println("Input string contains more than 6 hashes.");
inputValid = false;
}
return inputValid;
}
/*
* Splits the input markdown string into two strings representing hashes and content
* @Param String input - Input string representing markdown to be split
* @Return void
*/
public static void splitString(String input)
{
//Find index of space within input
int space = input.indexOf(" ");
//Split the string into hashes and content using the index of the string
hashes = input.substring(0, space);
content = input.substring(space+1, input.length());
//Trim leading spaces from both hashes and content
hashes = hashes.trim();
content = content.trim();
numHashes = hashes.length();
System.out.println("Hashes: " + hashes);
System.out.println("Content: " + content);
}
/*
* Constructs the output using the hashes and content strings produced by the splitSplit method
* @Param void
* @Return String output - String representation of HTML header
*/
public static String constructOutput()
{
String output = "<h";
output = output + Integer.toString(numHashes) + ">";
output = output + content;
output = output + "</h" + Integer.toString(numHashes) + ">";
System.out.println("Output:" + output);
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment